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:
lan
2025-12-02 22:52:33 +08:00
parent 792e96b238
commit 034e02e93a
54 changed files with 2305 additions and 2708 deletions

View File

@@ -5,6 +5,7 @@ import (
"carrotskin/internal/model"
"carrotskin/pkg/storage"
"context"
"time"
"go.uber.org/zap"
)
@@ -12,23 +13,23 @@ import (
// UserService 用户服务接口
type UserService interface {
// 用户认证
Register(username, password, email, avatar string) (*model.User, string, error)
Login(usernameOrEmail, password, ipAddress, userAgent string) (*model.User, string, error)
Register(ctx context.Context, username, password, email, avatar string) (*model.User, string, error)
Login(ctx context.Context, usernameOrEmail, password, ipAddress, userAgent string) (*model.User, string, error)
// 用户查询
GetByID(id int64) (*model.User, error)
GetByEmail(email string) (*model.User, error)
GetByID(ctx context.Context, id int64) (*model.User, error)
GetByEmail(ctx context.Context, email string) (*model.User, error)
// 用户更新
UpdateInfo(user *model.User) error
UpdateAvatar(userID int64, avatarURL string) error
ChangePassword(userID int64, oldPassword, newPassword string) error
ResetPassword(email, newPassword string) error
ChangeEmail(userID int64, newEmail string) error
UpdateInfo(ctx context.Context, user *model.User) error
UpdateAvatar(ctx context.Context, userID int64, avatarURL string) error
ChangePassword(ctx context.Context, userID int64, oldPassword, newPassword string) error
ResetPassword(ctx context.Context, email, newPassword string) error
ChangeEmail(ctx context.Context, userID int64, newEmail string) error
// URL验证
ValidateAvatarURL(avatarURL string) error
ValidateAvatarURL(ctx context.Context, avatarURL string) error
// 配置获取
GetMaxProfilesPerUser() int
GetMaxTexturesPerUser() int
@@ -37,51 +38,51 @@ type UserService interface {
// ProfileService 档案服务接口
type ProfileService interface {
// 档案CRUD
Create(userID int64, name string) (*model.Profile, error)
GetByUUID(uuid string) (*model.Profile, error)
GetByUserID(userID int64) ([]*model.Profile, error)
Update(uuid string, userID int64, name *string, skinID, capeID *int64) (*model.Profile, error)
Delete(uuid string, userID int64) error
Create(ctx context.Context, userID int64, name string) (*model.Profile, error)
GetByUUID(ctx context.Context, uuid string) (*model.Profile, error)
GetByUserID(ctx context.Context, userID int64) ([]*model.Profile, error)
Update(ctx context.Context, uuid string, userID int64, name *string, skinID, capeID *int64) (*model.Profile, error)
Delete(ctx context.Context, uuid string, userID int64) error
// 档案状态
SetActive(uuid string, userID int64) error
CheckLimit(userID int64, maxProfiles int) error
SetActive(ctx context.Context, uuid string, userID int64) error
CheckLimit(ctx context.Context, userID int64, maxProfiles int) error
// 批量查询
GetByNames(names []string) ([]*model.Profile, error)
GetByProfileName(name string) (*model.Profile, error)
GetByNames(ctx context.Context, names []string) ([]*model.Profile, error)
GetByProfileName(ctx context.Context, name string) (*model.Profile, error)
}
// TextureService 材质服务接口
type TextureService interface {
// 材质CRUD
Create(uploaderID int64, name, description, textureType, url, hash string, size int, isPublic, isSlim bool) (*model.Texture, error)
GetByID(id int64) (*model.Texture, error)
GetByUserID(uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error)
Search(keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error)
Update(textureID, uploaderID int64, name, description string, isPublic *bool) (*model.Texture, error)
Delete(textureID, uploaderID int64) error
Create(ctx context.Context, uploaderID int64, name, description, textureType, url, hash string, size int, isPublic, isSlim bool) (*model.Texture, error)
GetByID(ctx context.Context, id int64) (*model.Texture, error)
GetByUserID(ctx context.Context, uploaderID int64, page, pageSize int) ([]*model.Texture, int64, error)
Search(ctx context.Context, keyword string, textureType model.TextureType, publicOnly bool, page, pageSize int) ([]*model.Texture, int64, error)
Update(ctx context.Context, textureID, uploaderID int64, name, description string, isPublic *bool) (*model.Texture, error)
Delete(ctx context.Context, textureID, uploaderID int64) error
// 收藏
ToggleFavorite(userID, textureID int64) (bool, error)
GetUserFavorites(userID int64, page, pageSize int) ([]*model.Texture, int64, error)
ToggleFavorite(ctx context.Context, userID, textureID int64) (bool, error)
GetUserFavorites(ctx context.Context, userID int64, page, pageSize int) ([]*model.Texture, int64, error)
// 限制检查
CheckUploadLimit(uploaderID int64, maxTextures int) error
CheckUploadLimit(ctx context.Context, uploaderID int64, maxTextures int) error
}
// TokenService 令牌服务接口
type TokenService interface {
// 令牌管理
Create(userID int64, uuid, clientToken string) (*model.Profile, []*model.Profile, string, string, error)
Validate(accessToken, clientToken string) bool
Refresh(accessToken, clientToken, selectedProfileID string) (string, string, error)
Invalidate(accessToken string)
InvalidateUserTokens(userID int64)
Create(ctx context.Context, userID int64, uuid, clientToken string) (*model.Profile, []*model.Profile, string, string, error)
Validate(ctx context.Context, accessToken, clientToken string) bool
Refresh(ctx context.Context, accessToken, clientToken, selectedProfileID string) (string, string, error)
Invalidate(ctx context.Context, accessToken string)
InvalidateUserTokens(ctx context.Context, userID int64)
// 令牌查询
GetUUIDByAccessToken(accessToken string) (string, error)
GetUserIDByAccessToken(accessToken string) (int64, error)
GetUUIDByAccessToken(ctx context.Context, accessToken string) (string, error)
GetUserIDByAccessToken(ctx context.Context, accessToken string) (int64, error)
}
// VerificationService 验证码服务接口
@@ -105,23 +106,37 @@ type UploadService interface {
// YggdrasilService Yggdrasil服务接口
type YggdrasilService interface {
// 用户认证
GetUserIDByEmail(email string) (int64, error)
VerifyPassword(password string, userID int64) error
GetUserIDByEmail(ctx context.Context, email string) (int64, error)
VerifyPassword(ctx context.Context, password string, userID int64) error
// 会话管理
JoinServer(serverID, accessToken, selectedProfile, ip string) error
HasJoinedServer(serverID, username, ip string) error
JoinServer(ctx context.Context, serverID, accessToken, selectedProfile, ip string) error
HasJoinedServer(ctx context.Context, serverID, username, ip string) error
// 密码管理
ResetYggdrasilPassword(userID int64) (string, error)
ResetYggdrasilPassword(ctx context.Context, userID int64) (string, error)
// 序列化
SerializeProfile(profile model.Profile) map[string]interface{}
SerializeUser(user *model.User, uuid string) map[string]interface{}
SerializeProfile(ctx context.Context, profile model.Profile) map[string]interface{}
SerializeUser(ctx context.Context, user *model.User, uuid string) map[string]interface{}
// 证书
GeneratePlayerCertificate(uuid string) (map[string]interface{}, error)
GetPublicKey() (string, error)
GeneratePlayerCertificate(ctx context.Context, uuid string) (map[string]interface{}, error)
GetPublicKey(ctx context.Context) (string, error)
}
// SecurityService 安全服务接口
type SecurityService interface {
// 登录安全
CheckLoginLocked(ctx context.Context, identifier string) (bool, time.Duration, error)
RecordLoginFailure(ctx context.Context, identifier string) (int, error)
ClearLoginAttempts(ctx context.Context, identifier string) error
GetRemainingLoginAttempts(ctx context.Context, identifier string) (int, error)
// 验证码安全
CheckVerifyLocked(ctx context.Context, email, codeType string) (bool, time.Duration, error)
RecordVerifyFailure(ctx context.Context, email, codeType string) (int, error)
ClearVerifyAttempts(ctx context.Context, email, codeType string) error
}
// Services 服务集合
@@ -134,6 +149,7 @@ type Services struct {
Captcha CaptchaService
Upload UploadService
Yggdrasil YggdrasilService
Security SecurityService
}
// ServiceDeps 服务依赖
@@ -141,5 +157,3 @@ type ServiceDeps struct {
Logger *zap.Logger
Storage *storage.StorageClient
}