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) api.POST("/register", handlers.Register) protected.Use(middleware.AuthRequired()) { protected.DELETE("/items/:id", handlers.DeleteItem) protected.PATCH("/items/:id", handlers.UpdateItem) protected.POST("/items", handlers.AddItem) protected.GET("/cart/", handlers.GetCart) protected.POST("/cart/", handlers.AddToCart) protected.PATCH("/cart/:id", handlers.UpdateCartItem) protected.DELETE("/cart/:id", handlers.RemoveFromCart) protected.POST("/orders/", handlers.CreateOrder) protected.GET("/orders", handlers.GetUserOrders) protected.GET("/orders/:id", handlers.GetOrder) protected.PATCH("/profile", handlers.UpdateProfile) } admin := protected.Group("/admin") admin.Use(middleware.AdminOnly()) { admin.POST("/register", handlers.RegisterAdmin) admin.GET("/orders", handlers.GetAllOrders) admin.PATCH("/orders/:id", handlers.UpdateOrderStatus) } } r.NoRoute(func(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{"status": 404, "message": "API not found"}) }) r.Run(":8081") }