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

@@ -7,6 +7,9 @@ import (
// TestGenerateVerificationCode 测试生成验证码函数
func TestGenerateVerificationCode(t *testing.T) {
// 创建服务实例(使用 nil因为这个测试不需要依赖
svc := &verificationService{}
tests := []struct {
name string
wantLen int
@@ -21,18 +24,18 @@ func TestGenerateVerificationCode(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, err := GenerateVerificationCode()
code, err := svc.generateCode()
if (err != nil) != tt.wantErr {
t.Errorf("GenerateVerificationCode() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("generateCode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(code) != tt.wantLen {
t.Errorf("GenerateVerificationCode() code length = %v, want %v", len(code), tt.wantLen)
t.Errorf("generateCode() code length = %v, want %v", len(code), tt.wantLen)
}
// 验证验证码只包含数字
for _, c := range code {
if c < '0' || c > '9' {
t.Errorf("GenerateVerificationCode() code contains non-digit: %c", c)
t.Errorf("generateCode() code contains non-digit: %c", c)
}
}
})
@@ -41,9 +44,9 @@ func TestGenerateVerificationCode(t *testing.T) {
// 测试多次生成,验证码应该不同(概率上)
codes := make(map[string]bool)
for i := 0; i < 100; i++ {
code, err := GenerateVerificationCode()
code, err := svc.generateCode()
if err != nil {
t.Fatalf("GenerateVerificationCode() failed: %v", err)
t.Fatalf("generateCode() failed: %v", err)
}
if codes[code] {
t.Logf("发现重复验证码这是正常的因为只有6位数字: %s", code)
@@ -82,9 +85,10 @@ func TestVerificationConstants(t *testing.T) {
// TestVerificationCodeFormat 测试验证码格式
func TestVerificationCodeFormat(t *testing.T) {
code, err := GenerateVerificationCode()
svc := &verificationService{}
code, err := svc.generateCode()
if err != nil {
t.Fatalf("GenerateVerificationCode() failed: %v", err)
t.Fatalf("generateCode() failed: %v", err)
}
// 验证长度