refactor: Implement dependency injection for handlers and services
- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection. - Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection. - Updated the container to initialize service instances and provide them to handlers. - Enhanced code structure for better testability and adherence to Go best practices.
This commit is contained in:
@@ -1,142 +1,193 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/middleware"
|
||||
"carrotskin/internal/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RegisterRoutes 注册所有路由
|
||||
func RegisterRoutes(router *gin.Engine) {
|
||||
// Handlers 集中管理所有Handler
|
||||
type Handlers struct {
|
||||
Auth *AuthHandler
|
||||
User *UserHandler
|
||||
Texture *TextureHandler
|
||||
Profile *ProfileHandler
|
||||
Captcha *CaptchaHandler
|
||||
Yggdrasil *YggdrasilHandler
|
||||
}
|
||||
|
||||
// NewHandlers 创建所有Handler实例
|
||||
func NewHandlers(c *container.Container) *Handlers {
|
||||
return &Handlers{
|
||||
Auth: NewAuthHandler(c),
|
||||
User: NewUserHandler(c),
|
||||
Texture: NewTextureHandler(c),
|
||||
Profile: NewProfileHandler(c),
|
||||
Captcha: NewCaptchaHandler(c),
|
||||
Yggdrasil: NewYggdrasilHandler(c),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutesWithDI 使用依赖注入注册所有路由
|
||||
func RegisterRoutesWithDI(router *gin.Engine, c *container.Container) {
|
||||
// 设置Swagger文档
|
||||
SetupSwagger(router)
|
||||
|
||||
// 创建Handler实例
|
||||
h := NewHandlers(c)
|
||||
|
||||
// API路由组
|
||||
v1 := router.Group("/api/v1")
|
||||
{
|
||||
// 认证路由(无需JWT)
|
||||
authGroup := v1.Group("/auth")
|
||||
{
|
||||
authGroup.POST("/register", Register)
|
||||
authGroup.POST("/login", Login)
|
||||
authGroup.POST("/send-code", SendVerificationCode)
|
||||
authGroup.POST("/reset-password", ResetPassword)
|
||||
}
|
||||
registerAuthRoutes(v1, h.Auth)
|
||||
|
||||
// 用户路由(需要JWT认证)
|
||||
userGroup := v1.Group("/user")
|
||||
userGroup.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
userGroup.GET("/profile", GetUserProfile)
|
||||
userGroup.PUT("/profile", UpdateUserProfile)
|
||||
|
||||
// 头像相关
|
||||
userGroup.POST("/avatar/upload-url", GenerateAvatarUploadURL)
|
||||
userGroup.PUT("/avatar", UpdateAvatar)
|
||||
|
||||
// 更换邮箱
|
||||
userGroup.POST("/change-email", ChangeEmail)
|
||||
|
||||
// Yggdrasil密码相关
|
||||
userGroup.POST("/yggdrasil-password/reset", ResetYggdrasilPassword) // 重置Yggdrasil密码并返回新密码
|
||||
}
|
||||
registerUserRoutes(v1, h.User)
|
||||
|
||||
// 材质路由
|
||||
textureGroup := v1.Group("/texture")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
textureGroup.GET("", SearchTextures) // 搜索材质
|
||||
textureGroup.GET("/:id", GetTexture) // 获取材质详情
|
||||
|
||||
// 需要认证的路由
|
||||
textureAuth := textureGroup.Group("")
|
||||
textureAuth.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
textureAuth.POST("/upload-url", GenerateTextureUploadURL) // 生成上传URL
|
||||
textureAuth.POST("", CreateTexture) // 创建材质记录
|
||||
textureAuth.PUT("/:id", UpdateTexture) // 更新材质
|
||||
textureAuth.DELETE("/:id", DeleteTexture) // 删除材质
|
||||
textureAuth.POST("/:id/favorite", ToggleFavorite) // 切换收藏
|
||||
textureAuth.GET("/my", GetUserTextures) // 我的材质
|
||||
textureAuth.GET("/favorites", GetUserFavorites) // 我的收藏
|
||||
}
|
||||
}
|
||||
registerTextureRoutes(v1, h.Texture)
|
||||
|
||||
// 档案路由
|
||||
profileGroup := v1.Group("/profile")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
profileGroup.GET("/:uuid", GetProfile) // 获取档案详情
|
||||
registerProfileRoutesWithDI(v1, h.Profile)
|
||||
|
||||
// 需要认证的路由
|
||||
profileAuth := profileGroup.Group("")
|
||||
profileAuth.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
profileAuth.POST("/", CreateProfile) // 创建档案
|
||||
profileAuth.GET("/", GetProfiles) // 获取我的档案列表
|
||||
profileAuth.PUT("/:uuid", UpdateProfile) // 更新档案
|
||||
profileAuth.DELETE("/:uuid", DeleteProfile) // 删除档案
|
||||
profileAuth.POST("/:uuid/activate", SetActiveProfile) // 设置活跃档案
|
||||
}
|
||||
}
|
||||
// 验证码路由
|
||||
captchaGroup := v1.Group("/captcha")
|
||||
{
|
||||
captchaGroup.GET("/generate", Generate) //生成验证码
|
||||
captchaGroup.POST("/verify", Verify) //验证验证码
|
||||
}
|
||||
registerCaptchaRoutesWithDI(v1, h.Captcha)
|
||||
|
||||
// Yggdrasil API路由组
|
||||
ygg := v1.Group("/yggdrasil")
|
||||
{
|
||||
ygg.GET("", GetMetaData)
|
||||
ygg.POST("/minecraftservices/player/certificates", GetPlayerCertificates)
|
||||
authserver := ygg.Group("/authserver")
|
||||
{
|
||||
authserver.POST("/authenticate", Authenticate)
|
||||
authserver.POST("/validate", ValidToken)
|
||||
authserver.POST("/refresh", RefreshToken)
|
||||
authserver.POST("/invalidate", InvalidToken)
|
||||
authserver.POST("/signout", SignOut)
|
||||
}
|
||||
sessionServer := ygg.Group("/sessionserver")
|
||||
{
|
||||
sessionServer.GET("/session/minecraft/profile/:uuid", GetProfileByUUID)
|
||||
sessionServer.POST("/session/minecraft/join", JoinServer)
|
||||
sessionServer.GET("/session/minecraft/hasJoined", HasJoinedServer)
|
||||
}
|
||||
api := ygg.Group("/api")
|
||||
profiles := api.Group("/profiles")
|
||||
{
|
||||
profiles.POST("/minecraft", GetProfilesByName)
|
||||
}
|
||||
}
|
||||
registerYggdrasilRoutesWithDI(v1, h.Yggdrasil)
|
||||
|
||||
// 系统路由
|
||||
system := v1.Group("/system")
|
||||
registerSystemRoutes(v1)
|
||||
}
|
||||
}
|
||||
|
||||
// registerAuthRoutes 注册认证路由
|
||||
func registerAuthRoutes(v1 *gin.RouterGroup, h *AuthHandler) {
|
||||
authGroup := v1.Group("/auth")
|
||||
{
|
||||
authGroup.POST("/register", h.Register)
|
||||
authGroup.POST("/login", h.Login)
|
||||
authGroup.POST("/send-code", h.SendVerificationCode)
|
||||
authGroup.POST("/reset-password", h.ResetPassword)
|
||||
}
|
||||
}
|
||||
|
||||
// registerUserRoutes 注册用户路由
|
||||
func registerUserRoutes(v1 *gin.RouterGroup, h *UserHandler) {
|
||||
userGroup := v1.Group("/user")
|
||||
userGroup.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
userGroup.GET("/profile", h.GetProfile)
|
||||
userGroup.PUT("/profile", h.UpdateProfile)
|
||||
|
||||
// 头像相关
|
||||
userGroup.POST("/avatar/upload-url", h.GenerateAvatarUploadURL)
|
||||
userGroup.PUT("/avatar", h.UpdateAvatar)
|
||||
|
||||
// 更换邮箱
|
||||
userGroup.POST("/change-email", h.ChangeEmail)
|
||||
|
||||
// Yggdrasil密码相关
|
||||
userGroup.POST("/yggdrasil-password/reset", h.ResetYggdrasilPassword)
|
||||
}
|
||||
}
|
||||
|
||||
// registerTextureRoutes 注册材质路由
|
||||
func registerTextureRoutes(v1 *gin.RouterGroup, h *TextureHandler) {
|
||||
textureGroup := v1.Group("/texture")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
textureGroup.GET("", h.Search)
|
||||
textureGroup.GET("/:id", h.Get)
|
||||
|
||||
// 需要认证的路由
|
||||
textureAuth := textureGroup.Group("")
|
||||
textureAuth.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
system.GET("/config", GetSystemConfig)
|
||||
textureAuth.POST("/upload-url", h.GenerateUploadURL)
|
||||
textureAuth.POST("", h.Create)
|
||||
textureAuth.PUT("/:id", h.Update)
|
||||
textureAuth.DELETE("/:id", h.Delete)
|
||||
textureAuth.POST("/:id/favorite", h.ToggleFavorite)
|
||||
textureAuth.GET("/my", h.GetUserTextures)
|
||||
textureAuth.GET("/favorites", h.GetUserFavorites)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 以下是系统配置相关的占位符函数,待后续实现
|
||||
// registerProfileRoutesWithDI 注册档案路由(依赖注入版本)
|
||||
func registerProfileRoutesWithDI(v1 *gin.RouterGroup, h *ProfileHandler) {
|
||||
profileGroup := v1.Group("/profile")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
profileGroup.GET("/:uuid", h.Get)
|
||||
|
||||
// GetSystemConfig 获取系统配置
|
||||
// @Summary 获取系统配置
|
||||
// @Description 获取公开的系统配置信息
|
||||
// @Tags system
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.Response "获取成功"
|
||||
// @Router /api/v1/system/config [get]
|
||||
func GetSystemConfig(c *gin.Context) {
|
||||
// TODO: 实现从数据库读取系统配置
|
||||
c.JSON(200, model.NewSuccessResponse(gin.H{
|
||||
"site_name": "CarrotSkin",
|
||||
"site_description": "A Minecraft Skin Station",
|
||||
"registration_enabled": true,
|
||||
"max_textures_per_user": 100,
|
||||
"max_profiles_per_user": 5,
|
||||
}))
|
||||
// 需要认证的路由
|
||||
profileAuth := profileGroup.Group("")
|
||||
profileAuth.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
profileAuth.POST("/", h.Create)
|
||||
profileAuth.GET("/", h.List)
|
||||
profileAuth.PUT("/:uuid", h.Update)
|
||||
profileAuth.DELETE("/:uuid", h.Delete)
|
||||
profileAuth.POST("/:uuid/activate", h.SetActive)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// registerCaptchaRoutesWithDI 注册验证码路由(依赖注入版本)
|
||||
func registerCaptchaRoutesWithDI(v1 *gin.RouterGroup, h *CaptchaHandler) {
|
||||
captchaGroup := v1.Group("/captcha")
|
||||
{
|
||||
captchaGroup.GET("/generate", h.Generate)
|
||||
captchaGroup.POST("/verify", h.Verify)
|
||||
}
|
||||
}
|
||||
|
||||
// registerYggdrasilRoutesWithDI 注册Yggdrasil API路由(依赖注入版本)
|
||||
func registerYggdrasilRoutesWithDI(v1 *gin.RouterGroup, h *YggdrasilHandler) {
|
||||
ygg := v1.Group("/yggdrasil")
|
||||
{
|
||||
ygg.GET("", h.GetMetaData)
|
||||
ygg.POST("/minecraftservices/player/certificates", h.GetPlayerCertificates)
|
||||
authserver := ygg.Group("/authserver")
|
||||
{
|
||||
authserver.POST("/authenticate", h.Authenticate)
|
||||
authserver.POST("/validate", h.ValidToken)
|
||||
authserver.POST("/refresh", h.RefreshToken)
|
||||
authserver.POST("/invalidate", h.InvalidToken)
|
||||
authserver.POST("/signout", h.SignOut)
|
||||
}
|
||||
sessionServer := ygg.Group("/sessionserver")
|
||||
{
|
||||
sessionServer.GET("/session/minecraft/profile/:uuid", h.GetProfileByUUID)
|
||||
sessionServer.POST("/session/minecraft/join", h.JoinServer)
|
||||
sessionServer.GET("/session/minecraft/hasJoined", h.HasJoinedServer)
|
||||
}
|
||||
api := ygg.Group("/api")
|
||||
profiles := api.Group("/profiles")
|
||||
{
|
||||
profiles.POST("/minecraft", h.GetProfilesByName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// registerSystemRoutes 注册系统路由
|
||||
func registerSystemRoutes(v1 *gin.RouterGroup) {
|
||||
system := v1.Group("/system")
|
||||
{
|
||||
system.GET("/config", func(c *gin.Context) {
|
||||
// TODO: 实现从数据库读取系统配置
|
||||
c.JSON(200, model.NewSuccessResponse(gin.H{
|
||||
"site_name": "CarrotSkin",
|
||||
"site_description": "A Minecraft Skin Station",
|
||||
"registration_enabled": true,
|
||||
"max_textures_per_user": 100,
|
||||
"max_profiles_per_user": 5,
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user