Refactor: Implement uber-go/fx dependency injection

- Replace global variable pattern with proper dependency injection
- Add uber-go/fx for automatic dependency resolution
- Refactor all services and handlers to use constructor injection
- Eliminate fragile initialization order dependencies
- Improve testability and modularity
- Add structured logging with zap

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
2025-09-16 11:41:01 +03:30
co-authored by Qwen-Coder
parent d4044b0eaf
commit f136ae58b3
15 changed files with 389 additions and 255 deletions
+8 -16
View File
@@ -10,33 +10,25 @@ import (
)
type JwtTokenGenerator struct {
Key string
Issuer string
Audience string
cfg *config.Config
}
var JwtTokenGeneratorInstance *JwtTokenGenerator
func InitJwtTokenGenerator() {
JwtTokenGeneratorInstance = &JwtTokenGenerator{
Key: config.Config.JwtSecret,
Issuer: config.Config.JwtIssuer,
Audience: config.Config.JwtAudience,
}
func NewJwtTokenGenerator(cfg *config.Config) *JwtTokenGenerator {
return &JwtTokenGenerator{cfg: cfg}
}
func (j *JwtTokenGenerator) GenerateToken(claims map[string]interface{}) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"exp": time.Now().AddDate(0, 0, 1).Unix(),
"iat": time.Now().Unix(),
"iss": j.Issuer,
"aud": j.Audience,
"iss": j.cfg.JwtIssuer,
"aud": j.cfg.JwtAudience,
})
for k, v := range claims {
token.Claims.(jwt.MapClaims)[k] = v
}
return token.SignedString([]byte(j.Key))
return token.SignedString([]byte(j.cfg.JwtSecret))
}
// Gin middleware
@@ -53,7 +45,7 @@ func (j *JwtTokenGenerator) GinMiddleware() gin.HandlerFunc {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return []byte(j.Key), nil
return []byte(j.cfg.JwtSecret), nil
})
if err != nil {
@@ -69,4 +61,4 @@ func (j *JwtTokenGenerator) GinMiddleware() gin.HandlerFunc {
c.Next()
}
}
}