| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package router
- import (
- "net/http"
- "github.com/celestia-trace/backend/config"
- "github.com/celestia-trace/backend/handlers"
- "github.com/celestia-trace/backend/middleware"
- "github.com/gin-gonic/gin"
- "gorm.io/gorm"
- )
- // Setup creates and configures the gin router
- func Setup(db *gorm.DB, cfg *config.Config) *gin.Engine {
- gin.SetMode(cfg.GinMode)
- r := gin.Default()
- // CORS middleware for dev
- r.Use(func(c *gin.Context) {
- c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
- c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
- c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
- if c.Request.Method == "OPTIONS" {
- c.AbortWithStatus(204)
- return
- }
- c.Next()
- })
- v1 := r.Group("/v1")
- {
- v1.GET("/health", func(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": "healthy"})
- })
- authHandler := handlers.AuthHandler{DB: db, Cfg: cfg}
- authRoutes := v1.Group("/auth")
- {
- authRoutes.POST("/register", authHandler.Register)
- authRoutes.POST("/login", authHandler.Login)
-
- // Auth required
- authRoutes.Use(middleware.Auth(cfg))
- authRoutes.POST("/logout", authHandler.Logout)
- }
- userRoutes := v1.Group("/user")
- userRoutes.Use(middleware.Auth(cfg))
- {
- userRoutes.GET("/profile", authHandler.GetProfile)
- userRoutes.PUT("/profile", authHandler.UpdateProfile)
- userRoutes.POST("/change-password", authHandler.ChangePassword)
- }
- }
- return r
- }
|