package main import ( "log" "net/http" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "fishfish/database" "fishfish/handlers" "fishfish/middleware" ) func main() { database.InitDB() r := gin.Default() r.Static("/static", "./uploads") err := godotenv.Load() if err != nil { log.Println("Файл .env не был найден в папке backend, использую системные переменные") } // CORS Middleware r.Use(func(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Origin", "https://fish.kkhome.ru") c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return } c.Next() }) api := r.Group("/api") { api.POST("/login", handlers.Login) protected := api.Group("/") api.GET("/items/:id", handlers.GetItem) api.GET("/items/", handlers.GetAllItems) api.GET("/getCategories", handlers.GetCategories) protected.Use(middleware.AuthRequired()) { protected.DELETE("/items/:id", handlers.DeleteItem) protected.PATCH("/items/:id", handlers.UpdateItem) protected.POST("/items", handlers.AddItem) } admin := protected.Group("/") admin.Use(middleware.AdminOnly()) { admin.POST("/register", handlers.Register) } } r.NoRoute(func(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{"status": 404, "message": "API not found"}) }) r.Run(":8081") }