mengstack-api/internal/modules/auth/interfaces/routes.go
MengStack Dev fbf9af32f0 Initial commit: MengStack Go API framework
Clean public release with:
- Go/Gin/PostgreSQL/Redis architecture
- Multi-tenant support
- JWT authentication
- Docker Compose deployment
- Environment-based configuration
2026-10-03 00:14:40 +08:00

84 lines
1.9 KiB
Go

package interfaces
import (
"mengstack/internal/app/middleware"
"mengstack/internal/config"
"mengstack/internal/modules/auth/application"
"mengstack/internal/modules/auth/infrastructure"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"gorm.io/gorm"
)
type AuthMiddleware struct {
fx.In
Service *application.Service
}
func NewAuthMiddleware(in AuthMiddleware) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.AbortWithStatusJSON(401, gin.H{"code": -1, "message": "missing authorization"})
return
}
token := authHeader
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
token = authHeader[7:]
}
claims, err := application.ParseToken(token, in.Service.JWTSecret())
if err != nil {
c.AbortWithStatusJSON(401, gin.H{"code": -1, "message": "invalid token"})
return
}
c.Set("user_id", claims.UserID)
c.Set("tenant_id", claims.TenantID)
c.Next()
}
}
func SetupRoutes(r *gin.Engine, h *Handler, authMW gin.HandlerFunc) {
auth := r.Group("/api/v1/auth")
auth.Use(middleware.OptionalTenant())
{
auth.POST("/register", h.Register)
auth.POST("/login", h.Login)
auth.POST("/refresh", h.RefreshToken)
}
protected := r.Group("/api/v1")
protected.Use(authMW, middleware.MultiTenant())
{
protected.GET("/ping", h.Ping)
protected.GET("/profile", h.GetProfile)
protected.POST("/password", h.ChangePassword)
}
}
func NewJWTConfig(cfg *config.Config) application.JWTConfig {
return application.JWTConfig{
Secret: cfg.JWT.Secret,
AccessExpiryMinutes: cfg.JWT.AccessExpiryMinutes,
RefreshExpiryDays: cfg.JWT.RefreshExpiryDays,
Issuer: cfg.JWT.Issuer,
}
}
var Module = fx.Module("auth",
fx.Provide(
NewJWTConfig,
infrastructure.NewUserRepository,
application.NewService,
NewHandler,
NewAuthMiddleware,
),
fx.Invoke(func(db *gorm.DB) error {
return infrastructure.Migrate(db)
}),
)