Split the monolithic config.go (~120KB) into focused modules: - auth.go: JWT, TOTP, Turnstile, RateLimit configs - billing.go: Billing and Pricing configs - database.go: Database and Redis configs - gateway.go: Gateway and Upstream configs - gateway_sub.go: Gateway sub-configurations - ops_and_cache.go: Ops and Cache configs - platforms.go: Platform-specific configs - security.go: Security-related configs - server.go: Server configuration - config_defaults.go: Default values - config_defaults_detail.go: Detailed defaults - config_helpers.go: Helper functions - config_validate.go: Validation logic - config_validate_gateway.go: Gateway validation This improves: - Code maintainability and readability - Faster compilation (smaller files) - Easier navigation and debugging - Better separation of concerns
68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
package config
|
|
|
|
import "fmt"
|
|
|
|
// DatabaseConfig 数据库连接配置
|
|
// 性能优化:新增连接池参数,避免频繁创建/销毁连接
|
|
type DatabaseConfig struct {
|
|
Host string `mapstructure:"host"`
|
|
Port int `mapstructure:"port"`
|
|
User string `mapstructure:"user"`
|
|
Password string `mapstructure:"password"`
|
|
DBName string `mapstructure:"dbname"`
|
|
SSLMode string `mapstructure:"sslmode"`
|
|
// 连接池配置(性能优化:可配置化连接池参数)
|
|
MaxOpenConns int `mapstructure:"max_open_conns"` // 最大打开连接数,控制数据库连接上限
|
|
MaxIdleConns int `mapstructure:"max_idle_conns"` // 最大空闲连接数,保持热连接减少建连延迟
|
|
ConnMaxLifetimeMinutes int `mapstructure:"conn_max_lifetime_minutes"` // 连接最大存活时间
|
|
ConnMaxIdleTimeMinutes int `mapstructure:"conn_max_idle_time_minutes"` // 空闲连接最大存活时间
|
|
}
|
|
|
|
func (d *DatabaseConfig) DSN() string {
|
|
if d.Password == "" {
|
|
return fmt.Sprintf(
|
|
"host=%s port=%d user=%s dbname=%s sslmode=%s",
|
|
d.Host, d.Port, d.User, d.DBName, d.SSLMode,
|
|
)
|
|
}
|
|
return fmt.Sprintf(
|
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
|
d.Host, d.Port, d.User, d.Password, d.DBName, d.SSLMode,
|
|
)
|
|
}
|
|
|
|
// DSNWithTimezone returns DSN with timezone setting
|
|
func (d *DatabaseConfig) DSNWithTimezone(tz string) string {
|
|
if tz == "" {
|
|
tz = "Asia/Shanghai"
|
|
}
|
|
if d.Password == "" {
|
|
return fmt.Sprintf(
|
|
"host=%s port=%d user=%s dbname=%s sslmode=%s TimeZone=%s",
|
|
d.Host, d.Port, d.User, d.DBName, d.SSLMode, tz,
|
|
)
|
|
}
|
|
return fmt.Sprintf(
|
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s TimeZone=%s",
|
|
d.Host, d.Port, d.User, d.Password, d.DBName, d.SSLMode, tz,
|
|
)
|
|
}
|
|
|
|
// RedisConfig Redis 连接配置
|
|
type RedisConfig struct {
|
|
Host string `mapstructure:"host"`
|
|
Port int `mapstructure:"port"`
|
|
Password string `mapstructure:"password"`
|
|
DB int `mapstructure:"db"`
|
|
DialTimeoutSeconds int `mapstructure:"dial_timeout_seconds"` // 建立连接超时
|
|
ReadTimeoutSeconds int `mapstructure:"read_timeout_seconds"` // 读取超时
|
|
WriteTimeoutSeconds int `mapstructure:"write_timeout_seconds"` // 写入超时
|
|
PoolSize int `mapstructure:"pool_size"` // 连接池大小
|
|
MinIdleConns int `mapstructure:"min_idle_conns"` // 最小空闲连接数
|
|
EnableTLS bool `mapstructure:"enable_tls"` // 是否启用 TLS/SSL 连接
|
|
}
|
|
|
|
func (r *RedisConfig) Address() string {
|
|
return fmt.Sprintf("%s:%d", r.Host, r.Port)
|
|
}
|