package main import ( "log" "net/http" "os" "time" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "sproutgate-backend/internal/handlers" "sproutgate-backend/internal/storage" ) func main() { dataDir := os.Getenv("DATA_DIR") store, err := storage.NewStore(dataDir) if err != nil { log.Fatalf("failed to init storage: %v", err) } router := gin.Default() router.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Authorization", "X-Admin-Token"}, MaxAge: 12 * time.Hour, })) handler := handlers.NewHandler(store) router.GET("/api/health", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "status": "ok", "dataDir": store.DataDir(), }) }) router.GET("/api/docs", func(c *gin.Context) { c.File("API_DOCS.md") }) router.POST("/api/auth/login", handler.Login) router.POST("/api/auth/register", handler.Register) router.POST("/api/auth/verify-email", handler.VerifyEmail) router.POST("/api/auth/forgot-password", handler.ForgotPassword) router.POST("/api/auth/reset-password", handler.ResetPassword) router.POST("/api/auth/secondary-email/request", handler.RequestSecondaryEmail) router.POST("/api/auth/secondary-email/verify", handler.VerifySecondaryEmail) router.POST("/api/auth/verify", handler.Verify) router.GET("/api/auth/me", handler.Me) router.PUT("/api/auth/profile", handler.UpdateProfile) admin := router.Group("/api/admin") admin.Use(handler.AdminMiddleware()) admin.GET("/users", handler.ListUsers) admin.POST("/users", handler.CreateUser) admin.PUT("/users/:account", handler.UpdateUser) admin.DELETE("/users/:account", handler.DeleteUser) port := os.Getenv("PORT") if port == "" { port = "8080" } if err := router.Run(":" + port); err != nil { log.Fatalf("server stopped: %v", err) } }