Files
go-sqlc-htmx/routes/routes.go
2025-06-12 13:12:20 +03:00

43 lines
951 B
Go

package routes
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/yourusername/go-sqlc-jwt/config"
"github.com/yourusername/go-sqlc-jwt/controllers/user"
"github.com/yourusername/go-sqlc-jwt/db"
"github.com/yourusername/go-sqlc-jwt/internal/middleware"
)
func MainRouter(queries *db.Queries) *gin.Engine {
cfg := config.GetCfg()
r := gin.Default()
authHandler := user.NewAuthHandler(queries, cfg.JWT.Secret)
authGroup := r.Group("/auth")
{
authGroup.POST("/register", authHandler.Register)
authGroup.POST("/login", authHandler.Login)
}
// Protected routes
protected := r.Group("/api")
protected.Use(middleware.AuthMiddleware(cfg.JWT.Secret))
{
protected.GET("/profile", func(c *gin.Context) {
userID := c.MustGet("userID").(float64)
username := c.MustGet("username").(string)
c.JSON(http.StatusOK, gin.H{
"id": userID,
"username": username,
})
})
}
// user.UserRouter(r)
return r
}