basic frontend (#3)
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: aovantsev <aovantsev@avito.ru>
Reviewed-on: #3
This commit is contained in:
2025-10-03 16:25:14 +00:00
parent 78122bc996
commit ebf4bdeede
52 changed files with 21272 additions and 26 deletions

96
main.go
View File

@@ -2,24 +2,92 @@ package main
import (
"log"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
func main() {
// Load configuration with environment file precedence
config := LoadConfig()
// Set Gin mode based on configuration
gin.SetMode(config.GinMode)
// Initialize JWT with configuration
InitJWTWithConfig(config)
// Initialize database with configuration
if err := InitDBWithConfig(config); err != nil {
log.Fatal("Failed to initialize database :", err)
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello world"))
}
// Create tables
if err := CreateTables(); err != nil {
log.Fatal("Failed to create tables:", err)
}
func main() {
http.HandleFunc("/", helloHandler)
// Create Gin router
r := gin.Default()
port := ":8080"
log.Printf("Server starting on port %s", port)
log.Fatal(http.ListenAndServe(port, nil))
// Configure CORS
corsConfig := cors.DefaultConfig()
corsConfig.AllowOrigins = []string{"http://localhost:3000", "http://localhost:5173"} // React dev servers
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
corsConfig.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
corsConfig.AllowCredentials = true
r.Use(cors.New(corsConfig))
// Health check endpoint
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok"})
})
// API routes
api := r.Group("/api/v1")
{
// Authentication routes
auth := api.Group("/auth")
{
auth.POST("/register", RegisterHandler)
auth.POST("/login", LoginHandler)
auth.GET("/me", AuthMiddleware(), GetCurrentUserHandler)
}
// Counter routes (protected)
counters := api.Group("/counters")
counters.Use(AuthMiddleware())
{
counters.POST("", CreateCounterHandler)
counters.GET("", GetCountersHandler)
counters.GET("/:id", GetCounterHandler)
counters.PUT("/:id", UpdateCounterHandler)
counters.DELETE("/:id", DeleteCounterHandler)
counters.POST("/:id/increment", IncrementCounterHandler)
counters.GET("/:id/entries", GetCounterEntriesHandler)
counters.GET("/:id/stats", GetCounterStatsHandler)
}
}
// Serve static files (React app)
r.Static("/static", "./frontend/build/static")
r.StaticFile("/", "./frontend/build/index.html")
r.NoRoute(func(c *gin.Context) {
c.File("./frontend/build/index.html")
})
// Start server
port := config.Port
log.Printf("")
log.Printf("🚀 Starting Counter Application Server...")
log.Printf(" 🌐 Listening on: http://localhost:%s", port)
log.Printf(" 📊 Health check: http://localhost:%s/health", port)
log.Printf(" 🔗 API endpoint: http://localhost:%s/api/v1", port)
log.Printf(" 🎨 Frontend: http://localhost:%s/", port)
log.Printf("")
log.Printf("✅ Server is ready and accepting connections!")
log.Printf("")
log.Fatal(r.Run(":" + port))
}