|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gofaster/internal/auth/migration"
|
|
|
|
"gofaster/internal/auth/routes"
|
|
|
|
"gofaster/internal/core"
|
|
|
|
"gofaster/internal/shared/config"
|
|
|
|
"gofaster/internal/shared/database"
|
|
|
|
"gofaster/internal/shared/middleware"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthModule struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
db *gorm.DB
|
|
|
|
config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
core.RegisterModuleType(&AuthModule{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AuthModule) Name() string {
|
|
|
|
return "auth"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AuthModule) Init(config *config.Config, logger *zap.Logger, db *gorm.DB, redis *database.RedisClient) error {
|
|
|
|
fmt.Printf("🔍 AuthModule.Init 被调用\n")
|
|
|
|
m.logger = logger
|
|
|
|
m.db = db
|
|
|
|
m.config = config
|
|
|
|
|
|
|
|
fmt.Printf("✅ AuthModule 配置已设置\n")
|
|
|
|
// 运行数据库迁移
|
|
|
|
if err := migration.RunMigrations(db); err != nil {
|
|
|
|
logger.Error("Failed to run auth migrations", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("✅ 数据库迁移完成\n")
|
|
|
|
logger.Info("Auth module initialized successfully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AuthModule) RegisterRoutes(router *gin.RouterGroup) {
|
|
|
|
fmt.Printf("🔍 AuthModule.RegisterRoutes 被调用\n")
|
|
|
|
if m.db == nil {
|
|
|
|
m.logger.Error("Database connection not available for auth routes")
|
|
|
|
fmt.Printf("❌ 数据库连接不可用\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("✅ 数据库连接正常,开始注册认证路由\n")
|
|
|
|
// 注册认证路由
|
|
|
|
routes.RegisterAuthRoutes(router, m.db, middleware.JWTConfig{
|
|
|
|
SecretKey: m.config.JWT.Secret, // 从配置中获取JWT密钥
|
|
|
|
Issuer: m.config.JWT.Issuer, // 从配置中获取JWT发行者
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AuthModule) Cleanup() {
|
|
|
|
m.logger.Info("Cleaning up auth module")
|
|
|
|
}
|