feat: Enhance dependency injection and service integration
- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
This commit is contained in:
@@ -42,14 +42,14 @@ func (h *AuthHandler) Register(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证邮箱验证码
|
||||
if err := service.VerifyCode(c.Request.Context(), h.container.Redis, req.Email, req.VerificationCode, service.VerificationTypeRegister); err != nil {
|
||||
if err := h.container.VerificationService.VerifyCode(c.Request.Context(), req.Email, req.VerificationCode, service.VerificationTypeRegister); err != nil {
|
||||
h.logger.Warn("验证码验证失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// 注册用户
|
||||
user, token, err := h.container.UserService.Register(req.Username, req.Password, req.Email, req.Avatar)
|
||||
user, token, err := h.container.UserService.Register(c.Request.Context(), req.Username, req.Password, req.Email, req.Avatar)
|
||||
if err != nil {
|
||||
h.logger.Error("用户注册失败", zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
@@ -83,7 +83,7 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
ipAddress := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
|
||||
user, token, err := h.container.UserService.Login(req.Username, req.Password, ipAddress, userAgent)
|
||||
user, token, err := h.container.UserService.Login(c.Request.Context(), req.Username, req.Password, ipAddress, userAgent)
|
||||
if err != nil {
|
||||
h.logger.Warn("用户登录失败",
|
||||
zap.String("username_or_email", req.Username),
|
||||
@@ -117,13 +117,7 @@ func (h *AuthHandler) SendVerificationCode(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
emailService, err := h.getEmailService()
|
||||
if err != nil {
|
||||
RespondServerError(c, "邮件服务不可用", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.SendVerificationCode(c.Request.Context(), h.container.Redis, emailService, req.Email, req.Type); err != nil {
|
||||
if err := h.container.VerificationService.SendCode(c.Request.Context(), req.Email, req.Type); err != nil {
|
||||
h.logger.Error("发送验证码失败",
|
||||
zap.String("email", req.Email),
|
||||
zap.String("type", req.Type),
|
||||
@@ -154,14 +148,14 @@ func (h *AuthHandler) ResetPassword(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
if err := service.VerifyCode(c.Request.Context(), h.container.Redis, req.Email, req.VerificationCode, service.VerificationTypeResetPassword); err != nil {
|
||||
if err := h.container.VerificationService.VerifyCode(c.Request.Context(), req.Email, req.VerificationCode, service.VerificationTypeResetPassword); err != nil {
|
||||
h.logger.Warn("验证码验证失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
// 重置密码
|
||||
if err := h.container.UserService.ResetPassword(req.Email, req.NewPassword); err != nil {
|
||||
if err := h.container.UserService.ResetPassword(c.Request.Context(), req.Email, req.NewPassword); err != nil {
|
||||
h.logger.Error("重置密码失败", zap.String("email", req.Email), zap.Error(err))
|
||||
RespondServerError(c, err.Error(), nil)
|
||||
return
|
||||
|
||||
@@ -2,7 +2,6 @@ package handler
|
||||
|
||||
import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -39,7 +38,7 @@ type CaptchaVerifyRequest struct {
|
||||
// @Failure 500 {object} map[string]interface{} "生成失败"
|
||||
// @Router /api/v1/captcha/generate [get]
|
||||
func (h *CaptchaHandler) Generate(c *gin.Context) {
|
||||
masterImg, tileImg, captchaID, y, err := service.GenerateCaptchaData(c.Request.Context(), h.container.Redis)
|
||||
masterImg, tileImg, captchaID, y, err := h.container.CaptchaService.Generate(c.Request.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("生成验证码失败", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@@ -80,7 +79,7 @@ func (h *CaptchaHandler) Verify(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
valid, err := service.VerifyCaptchaData(c.Request.Context(), h.container.Redis, req.Dx, req.CaptchaID)
|
||||
valid, err := h.container.CaptchaService.Verify(c.Request.Context(), req.Dx, req.CaptchaID)
|
||||
if err != nil {
|
||||
h.logger.Error("验证码验证失败",
|
||||
zap.String("captcha_id", req.CaptchaID),
|
||||
@@ -105,5 +104,3 @@ func (h *CaptchaHandler) Verify(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -46,12 +46,12 @@ func (h *ProfileHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
maxProfiles := h.container.UserService.GetMaxProfilesPerUser()
|
||||
if err := h.container.ProfileService.CheckLimit(userID, maxProfiles); err != nil {
|
||||
if err := h.container.ProfileService.CheckLimit(c.Request.Context(), userID, maxProfiles); err != nil {
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
profile, err := h.container.ProfileService.Create(userID, req.Name)
|
||||
profile, err := h.container.ProfileService.Create(c.Request.Context(), userID, req.Name)
|
||||
if err != nil {
|
||||
h.logger.Error("创建档案失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -80,7 +80,7 @@ func (h *ProfileHandler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
profiles, err := h.container.ProfileService.GetByUserID(userID)
|
||||
profiles, err := h.container.ProfileService.GetByUserID(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
h.logger.Error("获取档案列表失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -110,7 +110,7 @@ func (h *ProfileHandler) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
profile, err := h.container.ProfileService.GetByUUID(uuid)
|
||||
profile, err := h.container.ProfileService.GetByUUID(c.Request.Context(), uuid)
|
||||
if err != nil {
|
||||
h.logger.Error("获取档案失败",
|
||||
zap.String("uuid", uuid),
|
||||
@@ -158,7 +158,7 @@ func (h *ProfileHandler) Update(c *gin.Context) {
|
||||
namePtr = &req.Name
|
||||
}
|
||||
|
||||
profile, err := h.container.ProfileService.Update(uuid, userID, namePtr, req.SkinID, req.CapeID)
|
||||
profile, err := h.container.ProfileService.Update(c.Request.Context(), uuid, userID, namePtr, req.SkinID, req.CapeID)
|
||||
if err != nil {
|
||||
h.logger.Error("更新档案失败",
|
||||
zap.String("uuid", uuid),
|
||||
@@ -195,7 +195,7 @@ func (h *ProfileHandler) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.ProfileService.Delete(uuid, userID); err != nil {
|
||||
if err := h.container.ProfileService.Delete(c.Request.Context(), uuid, userID); err != nil {
|
||||
h.logger.Error("删除档案失败",
|
||||
zap.String("uuid", uuid),
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -231,7 +231,7 @@ func (h *ProfileHandler) SetActive(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.ProfileService.SetActive(uuid, userID); err != nil {
|
||||
if err := h.container.ProfileService.SetActive(c.Request.Context(), uuid, userID); err != nil {
|
||||
h.logger.Error("设置活跃档案失败",
|
||||
zap.String("uuid", uuid),
|
||||
zap.Int64("user_id", userID),
|
||||
|
||||
@@ -3,7 +3,6 @@ package handler
|
||||
import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/internal/service"
|
||||
"carrotskin/internal/types"
|
||||
"strconv"
|
||||
|
||||
@@ -43,9 +42,8 @@ func (h *TextureHandler) GenerateUploadURL(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := service.GenerateTextureUploadURL(
|
||||
result, err := h.container.UploadService.GenerateTextureUploadURL(
|
||||
c.Request.Context(),
|
||||
h.container.Storage,
|
||||
userID,
|
||||
req.FileName,
|
||||
string(req.TextureType),
|
||||
@@ -83,12 +81,13 @@ func (h *TextureHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
maxTextures := h.container.UserService.GetMaxTexturesPerUser()
|
||||
if err := h.container.TextureService.CheckUploadLimit(userID, maxTextures); err != nil {
|
||||
if err := h.container.TextureService.CheckUploadLimit(c.Request.Context(), userID, maxTextures); err != nil {
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.Create(
|
||||
c.Request.Context(),
|
||||
userID,
|
||||
req.Name,
|
||||
req.Description,
|
||||
@@ -120,7 +119,7 @@ func (h *TextureHandler) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.GetByID(id)
|
||||
texture, err := h.container.TextureService.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
RespondNotFound(c, err.Error())
|
||||
return
|
||||
@@ -146,7 +145,7 @@ func (h *TextureHandler) Search(c *gin.Context) {
|
||||
textureType = model.TextureTypeCape
|
||||
}
|
||||
|
||||
textures, total, err := h.container.TextureService.Search(keyword, textureType, publicOnly, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.Search(c.Request.Context(), keyword, textureType, publicOnly, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("搜索材质失败", zap.String("keyword", keyword), zap.Error(err))
|
||||
RespondServerError(c, "搜索材质失败", err)
|
||||
@@ -175,7 +174,7 @@ func (h *TextureHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
texture, err := h.container.TextureService.Update(textureID, userID, req.Name, req.Description, req.IsPublic)
|
||||
texture, err := h.container.TextureService.Update(c.Request.Context(), textureID, userID, req.Name, req.Description, req.IsPublic)
|
||||
if err != nil {
|
||||
h.logger.Error("更新材质失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -202,7 +201,7 @@ func (h *TextureHandler) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.TextureService.Delete(textureID, userID); err != nil {
|
||||
if err := h.container.TextureService.Delete(c.Request.Context(), textureID, userID); err != nil {
|
||||
h.logger.Error("删除材质失败",
|
||||
zap.Int64("user_id", userID),
|
||||
zap.Int64("texture_id", textureID),
|
||||
@@ -228,7 +227,7 @@ func (h *TextureHandler) ToggleFavorite(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
isFavorited, err := h.container.TextureService.ToggleFavorite(userID, textureID)
|
||||
isFavorited, err := h.container.TextureService.ToggleFavorite(c.Request.Context(), userID, textureID)
|
||||
if err != nil {
|
||||
h.logger.Error("切换收藏状态失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -252,7 +251,7 @@ func (h *TextureHandler) GetUserTextures(c *gin.Context) {
|
||||
page := parseIntWithDefault(c.DefaultQuery("page", "1"), 1)
|
||||
pageSize := parseIntWithDefault(c.DefaultQuery("page_size", "20"), 20)
|
||||
|
||||
textures, total, err := h.container.TextureService.GetByUserID(userID, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.GetByUserID(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户材质列表失败", zap.Int64("user_id", userID), zap.Error(err))
|
||||
RespondServerError(c, "获取材质列表失败", err)
|
||||
@@ -272,7 +271,7 @@ func (h *TextureHandler) GetUserFavorites(c *gin.Context) {
|
||||
page := parseIntWithDefault(c.DefaultQuery("page", "1"), 1)
|
||||
pageSize := parseIntWithDefault(c.DefaultQuery("page_size", "20"), 20)
|
||||
|
||||
textures, total, err := h.container.TextureService.GetUserFavorites(userID, page, pageSize)
|
||||
textures, total, err := h.container.TextureService.GetUserFavorites(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户收藏列表失败", zap.Int64("user_id", userID), zap.Error(err))
|
||||
RespondServerError(c, "获取收藏列表失败", err)
|
||||
|
||||
@@ -30,7 +30,7 @@ func (h *UserHandler) GetProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByID(userID)
|
||||
user, err := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if err != nil || user == nil {
|
||||
h.logger.Error("获取用户信息失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -56,7 +56,7 @@ func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByID(userID)
|
||||
user, err := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if err != nil || user == nil {
|
||||
RespondNotFound(c, "用户不存在")
|
||||
return
|
||||
@@ -69,7 +69,7 @@ func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.UserService.ChangePassword(userID, req.OldPassword, req.NewPassword); err != nil {
|
||||
if err := h.container.UserService.ChangePassword(c.Request.Context(), userID, req.OldPassword, req.NewPassword); err != nil {
|
||||
h.logger.Error("修改密码失败", zap.Int64("user_id", userID), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
@@ -80,12 +80,12 @@ func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
||||
|
||||
// 更新头像
|
||||
if req.Avatar != "" {
|
||||
if err := h.container.UserService.ValidateAvatarURL(req.Avatar); err != nil {
|
||||
if err := h.container.UserService.ValidateAvatarURL(c.Request.Context(), req.Avatar); err != nil {
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
user.Avatar = req.Avatar
|
||||
if err := h.container.UserService.UpdateInfo(user); err != nil {
|
||||
if err := h.container.UserService.UpdateInfo(c.Request.Context(), user); err != nil {
|
||||
h.logger.Error("更新用户信息失败", zap.Int64("user_id", user.ID), zap.Error(err))
|
||||
RespondServerError(c, "更新失败", err)
|
||||
return
|
||||
@@ -93,7 +93,7 @@ func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 重新获取更新后的用户信息
|
||||
updatedUser, err := h.container.UserService.GetByID(userID)
|
||||
updatedUser, err := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if err != nil || updatedUser == nil {
|
||||
RespondNotFound(c, "用户不存在")
|
||||
return
|
||||
@@ -120,7 +120,7 @@ func (h *UserHandler) GenerateAvatarUploadURL(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := service.GenerateAvatarUploadURL(c.Request.Context(), h.container.Storage, userID, req.FileName)
|
||||
result, err := h.container.UploadService.GenerateAvatarUploadURL(c.Request.Context(), userID, req.FileName)
|
||||
if err != nil {
|
||||
h.logger.Error("生成头像上传URL失败",
|
||||
zap.Int64("user_id", userID),
|
||||
@@ -152,12 +152,12 @@ func (h *UserHandler) UpdateAvatar(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.UserService.ValidateAvatarURL(avatarURL); err != nil {
|
||||
if err := h.container.UserService.ValidateAvatarURL(c.Request.Context(), avatarURL); err != nil {
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.UserService.UpdateAvatar(userID, avatarURL); err != nil {
|
||||
if err := h.container.UserService.UpdateAvatar(c.Request.Context(), userID, avatarURL); err != nil {
|
||||
h.logger.Error("更新头像失败",
|
||||
zap.Int64("user_id", userID),
|
||||
zap.String("avatar_url", avatarURL),
|
||||
@@ -167,7 +167,7 @@ func (h *UserHandler) UpdateAvatar(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByID(userID)
|
||||
user, err := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if err != nil || user == nil {
|
||||
RespondNotFound(c, "用户不存在")
|
||||
return
|
||||
@@ -189,13 +189,13 @@ func (h *UserHandler) ChangeEmail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.VerifyCode(c.Request.Context(), h.container.Redis, req.NewEmail, req.VerificationCode, service.VerificationTypeChangeEmail); err != nil {
|
||||
if err := h.container.VerificationService.VerifyCode(c.Request.Context(), req.NewEmail, req.VerificationCode, service.VerificationTypeChangeEmail); err != nil {
|
||||
h.logger.Warn("验证码验证失败", zap.String("new_email", req.NewEmail), zap.Error(err))
|
||||
RespondBadRequest(c, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.container.UserService.ChangeEmail(userID, req.NewEmail); err != nil {
|
||||
if err := h.container.UserService.ChangeEmail(c.Request.Context(), userID, req.NewEmail); err != nil {
|
||||
h.logger.Error("更换邮箱失败",
|
||||
zap.Int64("user_id", userID),
|
||||
zap.String("new_email", req.NewEmail),
|
||||
@@ -205,7 +205,7 @@ func (h *UserHandler) ChangeEmail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByID(userID)
|
||||
user, err := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if err != nil || user == nil {
|
||||
RespondNotFound(c, "用户不存在")
|
||||
return
|
||||
@@ -221,7 +221,7 @@ func (h *UserHandler) ResetYggdrasilPassword(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
newPassword, err := service.ResetYggdrasilPassword(h.container.DB, userID)
|
||||
newPassword, err := h.container.YggdrasilService.ResetYggdrasilPassword(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
h.logger.Error("重置Yggdrasil密码失败", zap.Error(err), zap.Int64("userId", userID))
|
||||
RespondServerError(c, "重置Yggdrasil密码失败", nil)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/internal/service"
|
||||
"carrotskin/pkg/utils"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -189,9 +188,9 @@ func (h *YggdrasilHandler) Authenticate(c *gin.Context) {
|
||||
var UUID string
|
||||
|
||||
if emailRegex.MatchString(request.Identifier) {
|
||||
userId, err = service.GetUserIDByEmail(h.container.DB, request.Identifier)
|
||||
userId, err = h.container.YggdrasilService.GetUserIDByEmail(c.Request.Context(), request.Identifier)
|
||||
} else {
|
||||
profile, err = service.GetProfileByProfileName(h.container.DB, request.Identifier)
|
||||
profile, err = h.container.ProfileRepo.FindByName(request.Identifier)
|
||||
if err != nil {
|
||||
h.logger.Error("用户名不存在", zap.String("identifier", request.Identifier), zap.Error(err))
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
@@ -207,27 +206,27 @@ func (h *YggdrasilHandler) Authenticate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.VerifyPassword(h.container.DB, request.Password, userId); err != nil {
|
||||
if err := h.container.YggdrasilService.VerifyPassword(c.Request.Context(), request.Password, userId); err != nil {
|
||||
h.logger.Warn("认证失败: 密码错误", zap.Error(err))
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": ErrWrongPassword})
|
||||
return
|
||||
}
|
||||
|
||||
selectedProfile, availableProfiles, accessToken, clientToken, err := h.container.TokenService.Create(userId, UUID, request.ClientToken)
|
||||
selectedProfile, availableProfiles, accessToken, clientToken, err := h.container.TokenService.Create(c.Request.Context(), userId, UUID, request.ClientToken)
|
||||
if err != nil {
|
||||
h.logger.Error("生成令牌失败", zap.Error(err), zap.Int64("userId", userId))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByID(userId)
|
||||
user, err := h.container.UserService.GetByID(c.Request.Context(), userId)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户信息失败", zap.Error(err), zap.Int64("userId", userId))
|
||||
}
|
||||
|
||||
availableProfilesData := make([]map[string]interface{}, 0, len(availableProfiles))
|
||||
for _, p := range availableProfiles {
|
||||
availableProfilesData = append(availableProfilesData, service.SerializeProfile(h.container.DB, h.logger, h.container.Redis, *p))
|
||||
availableProfilesData = append(availableProfilesData, h.container.YggdrasilService.SerializeProfile(c.Request.Context(), *p))
|
||||
}
|
||||
|
||||
response := AuthenticateResponse{
|
||||
@@ -237,11 +236,11 @@ func (h *YggdrasilHandler) Authenticate(c *gin.Context) {
|
||||
}
|
||||
|
||||
if selectedProfile != nil {
|
||||
response.SelectedProfile = service.SerializeProfile(h.container.DB, h.logger, h.container.Redis, *selectedProfile)
|
||||
response.SelectedProfile = h.container.YggdrasilService.SerializeProfile(c.Request.Context(), *selectedProfile)
|
||||
}
|
||||
|
||||
if request.RequestUser && user != nil {
|
||||
response.User = service.SerializeUser(h.logger, user, UUID)
|
||||
response.User = h.container.YggdrasilService.SerializeUser(c.Request.Context(), user, UUID)
|
||||
}
|
||||
|
||||
h.logger.Info("用户认证成功", zap.Int64("userId", userId))
|
||||
@@ -257,7 +256,7 @@ func (h *YggdrasilHandler) ValidToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if h.container.TokenService.Validate(request.AccessToken, request.ClientToken) {
|
||||
if h.container.TokenService.Validate(c.Request.Context(), request.AccessToken, request.ClientToken) {
|
||||
h.logger.Info("令牌验证成功", zap.String("accessToken", request.AccessToken))
|
||||
c.JSON(http.StatusNoContent, gin.H{"valid": true})
|
||||
} else {
|
||||
@@ -275,17 +274,17 @@ func (h *YggdrasilHandler) RefreshToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
UUID, err := h.container.TokenService.GetUUIDByAccessToken(request.AccessToken)
|
||||
UUID, err := h.container.TokenService.GetUUIDByAccessToken(c.Request.Context(), request.AccessToken)
|
||||
if err != nil {
|
||||
h.logger.Warn("刷新令牌失败: 无效的访问令牌", zap.String("token", request.AccessToken), zap.Error(err))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.container.TokenService.GetUserIDByAccessToken(request.AccessToken)
|
||||
userID, _ := h.container.TokenService.GetUserIDByAccessToken(c.Request.Context(), request.AccessToken)
|
||||
UUID = utils.FormatUUID(UUID)
|
||||
|
||||
profile, err := h.container.ProfileService.GetByUUID(UUID)
|
||||
profile, err := h.container.ProfileService.GetByUUID(c.Request.Context(), UUID)
|
||||
if err != nil {
|
||||
h.logger.Error("刷新令牌失败: 无法获取用户信息", zap.Error(err))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -322,15 +321,15 @@ func (h *YggdrasilHandler) RefreshToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
profileData = service.SerializeProfile(h.container.DB, h.logger, h.container.Redis, *profile)
|
||||
profileData = h.container.YggdrasilService.SerializeProfile(c.Request.Context(), *profile)
|
||||
}
|
||||
|
||||
user, _ := h.container.UserService.GetByID(userID)
|
||||
user, _ := h.container.UserService.GetByID(c.Request.Context(), userID)
|
||||
if request.RequestUser && user != nil {
|
||||
userData = service.SerializeUser(h.logger, user, UUID)
|
||||
userData = h.container.YggdrasilService.SerializeUser(c.Request.Context(), user, UUID)
|
||||
}
|
||||
|
||||
newAccessToken, newClientToken, err := h.container.TokenService.Refresh(
|
||||
newAccessToken, newClientToken, err := h.container.TokenService.Refresh(c.Request.Context(),
|
||||
request.AccessToken,
|
||||
request.ClientToken,
|
||||
profileID,
|
||||
@@ -359,7 +358,7 @@ func (h *YggdrasilHandler) InvalidToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
h.container.TokenService.Invalidate(request.AccessToken)
|
||||
h.container.TokenService.Invalidate(c.Request.Context(), request.AccessToken)
|
||||
h.logger.Info("令牌已失效", zap.String("token", request.AccessToken))
|
||||
c.JSON(http.StatusNoContent, gin.H{})
|
||||
}
|
||||
@@ -379,20 +378,20 @@ func (h *YggdrasilHandler) SignOut(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.container.UserService.GetByEmail(request.Email)
|
||||
user, err := h.container.UserService.GetByEmail(c.Request.Context(), request.Email)
|
||||
if err != nil || user == nil {
|
||||
h.logger.Warn("登出失败: 用户不存在", zap.String("email", request.Email), zap.Error(err))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "用户不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := service.VerifyPassword(h.container.DB, request.Password, user.ID); err != nil {
|
||||
if err := h.container.YggdrasilService.VerifyPassword(c.Request.Context(), request.Password, user.ID); err != nil {
|
||||
h.logger.Warn("登出失败: 密码错误", zap.Int64("userId", user.ID))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": ErrWrongPassword})
|
||||
return
|
||||
}
|
||||
|
||||
h.container.TokenService.InvalidateUserTokens(user.ID)
|
||||
h.container.TokenService.InvalidateUserTokens(c.Request.Context(), user.ID)
|
||||
h.logger.Info("用户登出成功", zap.Int64("userId", user.ID))
|
||||
c.JSON(http.StatusNoContent, gin.H{"valid": true})
|
||||
}
|
||||
@@ -402,7 +401,7 @@ func (h *YggdrasilHandler) GetProfileByUUID(c *gin.Context) {
|
||||
uuid := utils.FormatUUID(c.Param("uuid"))
|
||||
h.logger.Info("获取配置文件请求", zap.String("uuid", uuid))
|
||||
|
||||
profile, err := h.container.ProfileService.GetByUUID(uuid)
|
||||
profile, err := h.container.ProfileService.GetByUUID(c.Request.Context(), uuid)
|
||||
if err != nil {
|
||||
h.logger.Error("获取配置文件失败", zap.Error(err), zap.String("uuid", uuid))
|
||||
standardResponse(c, http.StatusInternalServerError, nil, err.Error())
|
||||
@@ -410,7 +409,7 @@ func (h *YggdrasilHandler) GetProfileByUUID(c *gin.Context) {
|
||||
}
|
||||
|
||||
h.logger.Info("成功获取配置文件", zap.String("uuid", uuid), zap.String("name", profile.Name))
|
||||
c.JSON(http.StatusOK, service.SerializeProfile(h.container.DB, h.logger, h.container.Redis, *profile))
|
||||
c.JSON(http.StatusOK, h.container.YggdrasilService.SerializeProfile(c.Request.Context(), *profile))
|
||||
}
|
||||
|
||||
// JoinServer 加入服务器
|
||||
@@ -430,7 +429,7 @@ func (h *YggdrasilHandler) JoinServer(c *gin.Context) {
|
||||
zap.String("ip", clientIP),
|
||||
)
|
||||
|
||||
if err := service.JoinServer(h.container.DB, h.logger, h.container.Redis, request.ServerID, request.AccessToken, request.SelectedProfile, clientIP); err != nil {
|
||||
if err := h.container.YggdrasilService.JoinServer(c.Request.Context(), request.ServerID, request.AccessToken, request.SelectedProfile, clientIP); err != nil {
|
||||
h.logger.Error("加入服务器失败",
|
||||
zap.Error(err),
|
||||
zap.String("serverId", request.ServerID),
|
||||
@@ -473,7 +472,7 @@ func (h *YggdrasilHandler) HasJoinedServer(c *gin.Context) {
|
||||
zap.String("ip", clientIP),
|
||||
)
|
||||
|
||||
if err := service.HasJoinedServer(h.logger, h.container.Redis, serverID, username, clientIP); err != nil {
|
||||
if err := h.container.YggdrasilService.HasJoinedServer(c.Request.Context(), serverID, username, clientIP); err != nil {
|
||||
h.logger.Warn("会话验证失败",
|
||||
zap.Error(err),
|
||||
zap.String("serverId", serverID),
|
||||
@@ -484,7 +483,7 @@ func (h *YggdrasilHandler) HasJoinedServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
profile, err := h.container.ProfileService.GetByUUID(username)
|
||||
profile, err := h.container.ProfileService.GetByUUID(c.Request.Context(), username)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户配置文件失败", zap.Error(err), zap.String("username", username))
|
||||
standardResponse(c, http.StatusNoContent, nil, ErrProfileNotFound)
|
||||
@@ -496,7 +495,7 @@ func (h *YggdrasilHandler) HasJoinedServer(c *gin.Context) {
|
||||
zap.String("username", username),
|
||||
zap.String("uuid", profile.UUID),
|
||||
)
|
||||
c.JSON(200, service.SerializeProfile(h.container.DB, h.logger, h.container.Redis, *profile))
|
||||
c.JSON(200, h.container.YggdrasilService.SerializeProfile(c.Request.Context(), *profile))
|
||||
}
|
||||
|
||||
// GetProfilesByName 批量获取配置文件
|
||||
@@ -511,7 +510,7 @@ func (h *YggdrasilHandler) GetProfilesByName(c *gin.Context) {
|
||||
|
||||
h.logger.Info("接收到批量获取配置文件请求", zap.Int("count", len(names)))
|
||||
|
||||
profiles, err := h.container.ProfileService.GetByNames(names)
|
||||
profiles, err := h.container.ProfileService.GetByNames(c.Request.Context(), names)
|
||||
if err != nil {
|
||||
h.logger.Error("获取配置文件失败", zap.Error(err))
|
||||
}
|
||||
@@ -535,7 +534,7 @@ func (h *YggdrasilHandler) GetMetaData(c *gin.Context) {
|
||||
}
|
||||
|
||||
skinDomains := []string{".hitwh.games", ".littlelan.cn"}
|
||||
signature, err := service.GetPublicKeyFromRedisFunc(h.logger, h.container.Redis)
|
||||
signature, err := h.container.YggdrasilService.GetPublicKey(c.Request.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("获取公钥失败", zap.Error(err))
|
||||
standardResponse(c, http.StatusInternalServerError, nil, ErrInternalServer)
|
||||
@@ -573,7 +572,7 @@ func (h *YggdrasilHandler) GetPlayerCertificates(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
uuid, err := h.container.TokenService.GetUUIDByAccessToken(tokenID)
|
||||
uuid, err := h.container.TokenService.GetUUIDByAccessToken(c.Request.Context(), tokenID)
|
||||
if uuid == "" {
|
||||
h.logger.Error("获取玩家UUID失败", zap.Error(err))
|
||||
standardResponse(c, http.StatusInternalServerError, nil, ErrInternalServer)
|
||||
@@ -582,7 +581,7 @@ func (h *YggdrasilHandler) GetPlayerCertificates(c *gin.Context) {
|
||||
|
||||
uuid = utils.FormatUUID(uuid)
|
||||
|
||||
certificate, err := service.GeneratePlayerCertificate(h.container.DB, h.logger, h.container.Redis, uuid)
|
||||
certificate, err := h.container.YggdrasilService.GeneratePlayerCertificate(c.Request.Context(), uuid)
|
||||
if err != nil {
|
||||
h.logger.Error("生成玩家证书失败", zap.Error(err))
|
||||
standardResponse(c, http.StatusInternalServerError, nil, ErrInternalServer)
|
||||
|
||||
Reference in New Issue
Block a user