Initial commit: Basic project structure and dependencies
This commit is contained in:
166
internal/config/config.go
Normal file
166
internal/config/config.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config 应用程序总配置
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Database DatabaseConfig `mapstructure:"database"`
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
Cache CacheConfig `mapstructure:"cache"`
|
||||
Logger LoggerConfig `mapstructure:"logger"`
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
RateLimit RateLimitConfig `mapstructure:"ratelimit"`
|
||||
Monitoring MonitoringConfig `mapstructure:"monitoring"`
|
||||
Health HealthConfig `mapstructure:"health"`
|
||||
Resilience ResilienceConfig `mapstructure:"resilience"`
|
||||
Development DevelopmentConfig `mapstructure:"development"`
|
||||
App AppConfig `mapstructure:"app"`
|
||||
}
|
||||
|
||||
// ServerConfig HTTP服务器配置
|
||||
type ServerConfig struct {
|
||||
Port string `mapstructure:"port"`
|
||||
Mode string `mapstructure:"mode"`
|
||||
Host string `mapstructure:"host"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
||||
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
||||
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
||||
}
|
||||
|
||||
// DatabaseConfig 数据库配置
|
||||
type DatabaseConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port string `mapstructure:"port"`
|
||||
User string `mapstructure:"user"`
|
||||
Password string `mapstructure:"password"`
|
||||
Name string `mapstructure:"name"`
|
||||
SSLMode string `mapstructure:"sslmode"`
|
||||
Timezone string `mapstructure:"timezone"`
|
||||
MaxOpenConns int `mapstructure:"max_open_conns"`
|
||||
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
||||
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
// RedisConfig Redis配置
|
||||
type RedisConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port string `mapstructure:"port"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
PoolSize int `mapstructure:"pool_size"`
|
||||
MinIdleConns int `mapstructure:"min_idle_conns"`
|
||||
MaxRetries int `mapstructure:"max_retries"`
|
||||
DialTimeout time.Duration `mapstructure:"dial_timeout"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
||||
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
||||
}
|
||||
|
||||
// CacheConfig 缓存配置
|
||||
type CacheConfig struct {
|
||||
DefaultTTL time.Duration `mapstructure:"default_ttl"`
|
||||
CleanupInterval time.Duration `mapstructure:"cleanup_interval"`
|
||||
MaxSize int `mapstructure:"max_size"`
|
||||
}
|
||||
|
||||
// LoggerConfig 日志配置
|
||||
type LoggerConfig struct {
|
||||
Level string `mapstructure:"level"`
|
||||
Format string `mapstructure:"format"`
|
||||
Output string `mapstructure:"output"`
|
||||
FilePath string `mapstructure:"file_path"`
|
||||
MaxSize int `mapstructure:"max_size"`
|
||||
MaxBackups int `mapstructure:"max_backups"`
|
||||
MaxAge int `mapstructure:"max_age"`
|
||||
Compress bool `mapstructure:"compress"`
|
||||
}
|
||||
|
||||
// JWTConfig JWT配置
|
||||
type JWTConfig struct {
|
||||
Secret string `mapstructure:"secret"`
|
||||
ExpiresIn time.Duration `mapstructure:"expires_in"`
|
||||
RefreshExpiresIn time.Duration `mapstructure:"refresh_expires_in"`
|
||||
}
|
||||
|
||||
// RateLimitConfig 限流配置
|
||||
type RateLimitConfig struct {
|
||||
Requests int `mapstructure:"requests"`
|
||||
Window time.Duration `mapstructure:"window"`
|
||||
Burst int `mapstructure:"burst"`
|
||||
}
|
||||
|
||||
// MonitoringConfig 监控配置
|
||||
type MonitoringConfig struct {
|
||||
MetricsEnabled bool `mapstructure:"metrics_enabled"`
|
||||
MetricsPort string `mapstructure:"metrics_port"`
|
||||
TracingEnabled bool `mapstructure:"tracing_enabled"`
|
||||
TracingEndpoint string `mapstructure:"tracing_endpoint"`
|
||||
SampleRate float64 `mapstructure:"sample_rate"`
|
||||
}
|
||||
|
||||
// HealthConfig 健康检查配置
|
||||
type HealthConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Interval time.Duration `mapstructure:"interval"`
|
||||
Timeout time.Duration `mapstructure:"timeout"`
|
||||
}
|
||||
|
||||
// ResilienceConfig 容错配置
|
||||
type ResilienceConfig struct {
|
||||
CircuitBreakerEnabled bool `mapstructure:"circuit_breaker_enabled"`
|
||||
CircuitBreakerThreshold int `mapstructure:"circuit_breaker_threshold"`
|
||||
CircuitBreakerTimeout time.Duration `mapstructure:"circuit_breaker_timeout"`
|
||||
RetryMaxAttempts int `mapstructure:"retry_max_attempts"`
|
||||
RetryInitialDelay time.Duration `mapstructure:"retry_initial_delay"`
|
||||
RetryMaxDelay time.Duration `mapstructure:"retry_max_delay"`
|
||||
}
|
||||
|
||||
// DevelopmentConfig 开发配置
|
||||
type DevelopmentConfig struct {
|
||||
Debug bool `mapstructure:"debug"`
|
||||
EnableProfiler bool `mapstructure:"enable_profiler"`
|
||||
EnableCors bool `mapstructure:"enable_cors"`
|
||||
CorsOrigins string `mapstructure:"cors_allowed_origins"`
|
||||
CorsMethods string `mapstructure:"cors_allowed_methods"`
|
||||
CorsHeaders string `mapstructure:"cors_allowed_headers"`
|
||||
}
|
||||
|
||||
// AppConfig 应用程序配置
|
||||
type AppConfig struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Version string `mapstructure:"version"`
|
||||
Env string `mapstructure:"env"`
|
||||
}
|
||||
|
||||
// GetDSN 获取数据库DSN连接字符串
|
||||
func (d DatabaseConfig) GetDSN() string {
|
||||
return "host=" + d.Host +
|
||||
" user=" + d.User +
|
||||
" password=" + d.Password +
|
||||
" dbname=" + d.Name +
|
||||
" port=" + d.Port +
|
||||
" sslmode=" + d.SSLMode +
|
||||
" TimeZone=" + d.Timezone
|
||||
}
|
||||
|
||||
// GetRedisAddr 获取Redis地址
|
||||
func (r RedisConfig) GetRedisAddr() string {
|
||||
return r.Host + ":" + r.Port
|
||||
}
|
||||
|
||||
// IsProduction 检查是否为生产环境
|
||||
func (a AppConfig) IsProduction() bool {
|
||||
return a.Env == "production"
|
||||
}
|
||||
|
||||
// IsDevelopment 检查是否为开发环境
|
||||
func (a AppConfig) IsDevelopment() bool {
|
||||
return a.Env == "development"
|
||||
}
|
||||
|
||||
// IsStaging 检查是否为测试环境
|
||||
func (a AppConfig) IsStaging() bool {
|
||||
return a.Env == "staging"
|
||||
}
|
||||
Reference in New Issue
Block a user