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:
@@ -2,9 +2,12 @@ package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"carrotskin/pkg/config"
|
||||
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
@@ -22,19 +25,23 @@ func New(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
cfg.Timezone,
|
||||
)
|
||||
|
||||
// 配置GORM日志级别
|
||||
var gormLogLevel logger.LogLevel
|
||||
switch {
|
||||
case cfg.Driver == "postgres":
|
||||
gormLogLevel = logger.Info
|
||||
default:
|
||||
gormLogLevel = logger.Silent
|
||||
}
|
||||
// 配置慢查询监控
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: 200 * time.Millisecond, // 慢查询阈值:200ms
|
||||
LogLevel: logger.Warn, // 只记录警告和错误
|
||||
IgnoreRecordNotFoundError: true, // 忽略记录未找到错误
|
||||
Colorful: false, // 生产环境禁用彩色
|
||||
},
|
||||
)
|
||||
|
||||
// 打开数据库连接
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(gormLogLevel),
|
||||
DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束,避免循环依赖问题
|
||||
Logger: newLogger,
|
||||
DisableForeignKeyConstraintWhenMigrating: true, // 禁用外键约束
|
||||
PrepareStmt: true, // 启用预编译语句缓存
|
||||
QueryFields: true, // 明确指定查询字段
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("连接PostgreSQL数据库失败: %w", err)
|
||||
@@ -46,10 +53,26 @@ func New(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
return nil, fmt.Errorf("获取数据库实例失败: %w", err)
|
||||
}
|
||||
|
||||
// 配置连接池
|
||||
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
||||
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
|
||||
// 优化连接池配置
|
||||
maxIdleConns := cfg.MaxIdleConns
|
||||
if maxIdleConns <= 0 {
|
||||
maxIdleConns = 10
|
||||
}
|
||||
|
||||
maxOpenConns := cfg.MaxOpenConns
|
||||
if maxOpenConns <= 0 {
|
||||
maxOpenConns = 100
|
||||
}
|
||||
|
||||
connMaxLifetime := cfg.ConnMaxLifetime
|
||||
if connMaxLifetime <= 0 {
|
||||
connMaxLifetime = 1 * time.Hour
|
||||
}
|
||||
|
||||
sqlDB.SetMaxIdleConns(maxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(maxOpenConns)
|
||||
sqlDB.SetConnMaxLifetime(connMaxLifetime)
|
||||
sqlDB.SetConnMaxIdleTime(10 * time.Minute)
|
||||
|
||||
// 测试连接
|
||||
if err := sqlDB.Ping(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user