Files
counter/main.go
aovantsev d0f14dfca2
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
add env files
2025-10-03 13:33:22 +03:00

94 lines
2.5 KiB
Go

package main
import (
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
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)
}
// Create tables
if err := CreateTables(); err != nil {
log.Fatal("Failed to create tables:", err)
}
// Create Gin router
r := gin.Default()
// 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))
}