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:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -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)