refactor: Update service and repository methods to use context
- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles. - Updated handlers to utilize context in method calls, improving error handling and performance. - Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
@@ -219,7 +219,7 @@ func (h *CustomSkinHandler) GetTexture(c *gin.Context) {
|
||||
|
||||
// 增加下载计数(异步)
|
||||
go func() {
|
||||
_ = h.container.TextureRepo.IncrementDownloadCount(texture.ID)
|
||||
_ = h.container.TextureRepo.IncrementDownloadCount(ctx, texture.ID)
|
||||
}()
|
||||
|
||||
// 流式传输文件内容
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrotskin/internal/errors"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/internal/types"
|
||||
"net/http"
|
||||
@@ -165,17 +166,46 @@ func RespondSuccess(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, model.NewSuccessResponse(data))
|
||||
}
|
||||
|
||||
// RespondWithError 根据错误消息自动选择状态码
|
||||
// RespondWithError 根据错误类型自动选择状态码
|
||||
func RespondWithError(c *gin.Context, err error) {
|
||||
msg := err.Error()
|
||||
switch msg {
|
||||
case "档案不存在", "用户不存在", "材质不存在":
|
||||
RespondNotFound(c, msg)
|
||||
case "无权操作此档案", "无权操作此材质":
|
||||
RespondForbidden(c, msg)
|
||||
case "未授权":
|
||||
RespondUnauthorized(c, msg)
|
||||
default:
|
||||
RespondServerError(c, msg, nil)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 使用errors.Is检查预定义错误
|
||||
if errors.Is(err, errors.ErrUserNotFound) ||
|
||||
errors.Is(err, errors.ErrProfileNotFound) ||
|
||||
errors.Is(err, errors.ErrTextureNotFound) ||
|
||||
errors.Is(err, errors.ErrNotFound) {
|
||||
RespondNotFound(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if errors.Is(err, errors.ErrProfileNoPermission) ||
|
||||
errors.Is(err, errors.ErrTextureNoPermission) ||
|
||||
errors.Is(err, errors.ErrForbidden) {
|
||||
RespondForbidden(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if errors.Is(err, errors.ErrUnauthorized) ||
|
||||
errors.Is(err, errors.ErrInvalidToken) ||
|
||||
errors.Is(err, errors.ErrTokenExpired) {
|
||||
RespondUnauthorized(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 检查AppError类型
|
||||
var appErr *errors.AppError
|
||||
if errors.As(err, &appErr) {
|
||||
c.JSON(appErr.Code, model.NewErrorResponse(
|
||||
appErr.Code,
|
||||
appErr.Message,
|
||||
appErr.Err,
|
||||
))
|
||||
return
|
||||
}
|
||||
|
||||
// 默认返回500错误
|
||||
RespondServerError(c, err.Error(), err)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/middleware"
|
||||
"carrotskin/internal/model"
|
||||
"carrotskin/pkg/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -47,13 +48,13 @@ func RegisterRoutesWithDI(router *gin.Engine, c *container.Container) {
|
||||
registerAuthRoutes(v1, h.Auth)
|
||||
|
||||
// 用户路由(需要JWT认证)
|
||||
registerUserRoutes(v1, h.User)
|
||||
registerUserRoutes(v1, h.User, c.JWT)
|
||||
|
||||
// 材质路由
|
||||
registerTextureRoutes(v1, h.Texture)
|
||||
registerTextureRoutes(v1, h.Texture, c.JWT)
|
||||
|
||||
// 档案路由
|
||||
registerProfileRoutesWithDI(v1, h.Profile)
|
||||
registerProfileRoutesWithDI(v1, h.Profile, c.JWT)
|
||||
|
||||
// 验证码路由
|
||||
registerCaptchaRoutesWithDI(v1, h.Captcha)
|
||||
@@ -81,9 +82,9 @@ func registerAuthRoutes(v1 *gin.RouterGroup, h *AuthHandler) {
|
||||
}
|
||||
|
||||
// registerUserRoutes 注册用户路由
|
||||
func registerUserRoutes(v1 *gin.RouterGroup, h *UserHandler) {
|
||||
func registerUserRoutes(v1 *gin.RouterGroup, h *UserHandler, jwtService *auth.JWTService) {
|
||||
userGroup := v1.Group("/user")
|
||||
userGroup.Use(middleware.AuthMiddleware())
|
||||
userGroup.Use(middleware.AuthMiddleware(jwtService))
|
||||
{
|
||||
userGroup.GET("/profile", h.GetProfile)
|
||||
userGroup.PUT("/profile", h.UpdateProfile)
|
||||
@@ -101,7 +102,7 @@ func registerUserRoutes(v1 *gin.RouterGroup, h *UserHandler) {
|
||||
}
|
||||
|
||||
// registerTextureRoutes 注册材质路由
|
||||
func registerTextureRoutes(v1 *gin.RouterGroup, h *TextureHandler) {
|
||||
func registerTextureRoutes(v1 *gin.RouterGroup, h *TextureHandler, jwtService *auth.JWTService) {
|
||||
textureGroup := v1.Group("/texture")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
@@ -110,7 +111,7 @@ func registerTextureRoutes(v1 *gin.RouterGroup, h *TextureHandler) {
|
||||
|
||||
// 需要认证的路由
|
||||
textureAuth := textureGroup.Group("")
|
||||
textureAuth.Use(middleware.AuthMiddleware())
|
||||
textureAuth.Use(middleware.AuthMiddleware(jwtService))
|
||||
{
|
||||
textureAuth.POST("/upload-url", h.GenerateUploadURL)
|
||||
textureAuth.POST("", h.Create)
|
||||
@@ -124,7 +125,7 @@ func registerTextureRoutes(v1 *gin.RouterGroup, h *TextureHandler) {
|
||||
}
|
||||
|
||||
// registerProfileRoutesWithDI 注册档案路由(依赖注入版本)
|
||||
func registerProfileRoutesWithDI(v1 *gin.RouterGroup, h *ProfileHandler) {
|
||||
func registerProfileRoutesWithDI(v1 *gin.RouterGroup, h *ProfileHandler, jwtService *auth.JWTService) {
|
||||
profileGroup := v1.Group("/profile")
|
||||
{
|
||||
// 公开路由(无需认证)
|
||||
@@ -132,7 +133,7 @@ func registerProfileRoutesWithDI(v1 *gin.RouterGroup, h *ProfileHandler) {
|
||||
|
||||
// 需要认证的路由
|
||||
profileAuth := profileGroup.Group("")
|
||||
profileAuth.Use(middleware.AuthMiddleware())
|
||||
profileAuth.Use(middleware.AuthMiddleware(jwtService))
|
||||
{
|
||||
profileAuth.POST("/", h.Create)
|
||||
profileAuth.GET("/", h.List)
|
||||
|
||||
@@ -1,15 +1,95 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"carrotskin/pkg/database"
|
||||
"carrotskin/pkg/redis"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HealthCheck 健康检查
|
||||
// HealthCheck 健康检查,检查依赖服务状态
|
||||
func HealthCheck(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "ok",
|
||||
"message": "CarrotSkin API is running",
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
checks := make(map[string]string)
|
||||
status := "ok"
|
||||
|
||||
// 检查数据库
|
||||
if err := checkDatabase(ctx); err != nil {
|
||||
checks["database"] = "unhealthy: " + err.Error()
|
||||
status = "degraded"
|
||||
} else {
|
||||
checks["database"] = "healthy"
|
||||
}
|
||||
|
||||
// 检查Redis
|
||||
if err := checkRedis(ctx); err != nil {
|
||||
checks["redis"] = "unhealthy: " + err.Error()
|
||||
status = "degraded"
|
||||
} else {
|
||||
checks["redis"] = "healthy"
|
||||
}
|
||||
|
||||
// 根据状态返回相应的HTTP状态码
|
||||
httpStatus := http.StatusOK
|
||||
if status == "degraded" {
|
||||
httpStatus = http.StatusServiceUnavailable
|
||||
}
|
||||
|
||||
c.JSON(httpStatus, gin.H{
|
||||
"status": status,
|
||||
"message": "CarrotSkin API health check",
|
||||
"checks": checks,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
// checkDatabase 检查数据库连接
|
||||
func checkDatabase(ctx context.Context) error {
|
||||
db, err := database.GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 使用Ping检查连接
|
||||
if err := sqlDB.PingContext(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 执行简单查询验证
|
||||
var result int
|
||||
if err := db.WithContext(ctx).Raw("SELECT 1").Scan(&result).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkRedis 检查Redis连接
|
||||
func checkRedis(ctx context.Context) error {
|
||||
client, err := redis.GetClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if client == nil {
|
||||
return errors.New("Redis客户端未初始化")
|
||||
}
|
||||
|
||||
// 使用Ping检查连接
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ func (h *YggdrasilHandler) Authenticate(c *gin.Context) {
|
||||
if emailRegex.MatchString(request.Identifier) {
|
||||
userId, err = h.container.YggdrasilService.GetUserIDByEmail(c.Request.Context(), request.Identifier)
|
||||
} else {
|
||||
profile, err = h.container.ProfileRepo.FindByName(request.Identifier)
|
||||
profile, err = h.container.ProfileRepo.FindByName(c.Request.Context(), 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()})
|
||||
|
||||
Reference in New Issue
Block a user