Files
social-rating/backend/internal/server/router.go
2026-04-18 10:21:51 +03:00

50 lines
1.6 KiB
Go

package server
import (
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"social-raiting.nekiiinkognito.ru/internal/auth"
"social-raiting.nekiiinkognito.ru/internal/config"
"social-raiting.nekiiinkognito.ru/internal/socialrating"
)
func NewRouter(db *gorm.DB, cfg config.Config) *gin.Engine {
router := gin.Default()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173", "http://127.0.0.1:5173", "http://localhost:8081", "http://127.0.0.1:8081", "https://social-rating.nekiiinkognito.ru/"},
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.GET("/ping", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"message": "ok"})
})
router.StaticFile("/swagger.yaml", "./docs/swagger.yaml")
authHandler := auth.NewHandler(db, cfg.JWTSecret)
socialRatingHandler := socialrating.NewHandler(socialrating.NewService(db))
api := router.Group("/api")
api.POST("/auth/login", authHandler.Login)
protected := api.Group("/")
protected.Use(auth.Middleware(cfg.JWTSecret))
protected.GET("/auth/me", authHandler.Me)
protected.POST("/social-rating/increase", socialRatingHandler.Increase)
protected.POST("/social-rating/decrease", socialRatingHandler.Decrease)
admin := protected.Group("/")
admin.Use(auth.RequireAdmin(db))
admin.POST("/auth/register", authHandler.Register)
return router
}