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:
@@ -14,8 +14,8 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// tokenServiceImpl TokenService的实现
|
||||
type tokenServiceImpl struct {
|
||||
// tokenService TokenService的实现
|
||||
type tokenService struct {
|
||||
tokenRepo repository.TokenRepository
|
||||
profileRepo repository.ProfileRepository
|
||||
logger *zap.Logger
|
||||
@@ -27,7 +27,7 @@ func NewTokenService(
|
||||
profileRepo repository.ProfileRepository,
|
||||
logger *zap.Logger,
|
||||
) TokenService {
|
||||
return &tokenServiceImpl{
|
||||
return &tokenService{
|
||||
tokenRepo: tokenRepo,
|
||||
profileRepo: profileRepo,
|
||||
logger: logger,
|
||||
@@ -39,7 +39,7 @@ const (
|
||||
tokensMaxCount = 10
|
||||
)
|
||||
|
||||
func (s *tokenServiceImpl) Create(userID int64, UUID string, clientToken string) (*model.Profile, []*model.Profile, string, string, error) {
|
||||
func (s *tokenService) Create(ctx context.Context, userID int64, UUID string, clientToken string) (*model.Profile, []*model.Profile, string, string, error) {
|
||||
var (
|
||||
selectedProfileID *model.Profile
|
||||
availableProfiles []*model.Profile
|
||||
@@ -96,7 +96,7 @@ func (s *tokenServiceImpl) Create(userID int64, UUID string, clientToken string)
|
||||
return selectedProfileID, availableProfiles, accessToken, clientToken, nil
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) Validate(accessToken, clientToken string) bool {
|
||||
func (s *tokenService) Validate(ctx context.Context, accessToken, clientToken string) bool {
|
||||
if accessToken == "" {
|
||||
return false
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func (s *tokenServiceImpl) Validate(accessToken, clientToken string) bool {
|
||||
return token.ClientToken == clientToken
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) Refresh(accessToken, clientToken, selectedProfileID string) (string, string, error) {
|
||||
func (s *tokenService) Refresh(ctx context.Context, accessToken, clientToken, selectedProfileID string) (string, string, error) {
|
||||
if accessToken == "" {
|
||||
return "", "", errors.New("accessToken不能为空")
|
||||
}
|
||||
@@ -193,7 +193,7 @@ func (s *tokenServiceImpl) Refresh(accessToken, clientToken, selectedProfileID s
|
||||
return newAccessToken, oldToken.ClientToken, nil
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) Invalidate(accessToken string) {
|
||||
func (s *tokenService) Invalidate(ctx context.Context, accessToken string) {
|
||||
if accessToken == "" {
|
||||
return
|
||||
}
|
||||
@@ -206,7 +206,7 @@ func (s *tokenServiceImpl) Invalidate(accessToken string) {
|
||||
s.logger.Info("成功删除Token", zap.String("token", accessToken))
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) InvalidateUserTokens(userID int64) {
|
||||
func (s *tokenService) InvalidateUserTokens(ctx context.Context, userID int64) {
|
||||
if userID == 0 {
|
||||
return
|
||||
}
|
||||
@@ -220,17 +220,17 @@ func (s *tokenServiceImpl) InvalidateUserTokens(userID int64) {
|
||||
s.logger.Info("成功删除用户Token", zap.Int64("userId", userID))
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) GetUUIDByAccessToken(accessToken string) (string, error) {
|
||||
func (s *tokenService) GetUUIDByAccessToken(ctx context.Context, accessToken string) (string, error) {
|
||||
return s.tokenRepo.GetUUIDByAccessToken(accessToken)
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) GetUserIDByAccessToken(accessToken string) (int64, error) {
|
||||
func (s *tokenService) GetUserIDByAccessToken(ctx context.Context, accessToken string) (int64, error) {
|
||||
return s.tokenRepo.GetUserIDByAccessToken(accessToken)
|
||||
}
|
||||
|
||||
// 私有辅助方法
|
||||
|
||||
func (s *tokenServiceImpl) checkAndCleanupExcessTokens(userID int64) {
|
||||
func (s *tokenService) checkAndCleanupExcessTokens(userID int64) {
|
||||
if userID == 0 {
|
||||
return
|
||||
}
|
||||
@@ -261,7 +261,7 @@ func (s *tokenServiceImpl) checkAndCleanupExcessTokens(userID int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *tokenServiceImpl) validateProfileByUserID(userID int64, UUID string) (bool, error) {
|
||||
func (s *tokenService) validateProfileByUserID(userID int64, UUID string) (bool, error) {
|
||||
if userID == 0 || UUID == "" {
|
||||
return false, errors.New("用户ID或配置文件ID不能为空")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user