init
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Version string `yaml:"version"`
|
||||
Debug bool `yaml:"debug"`
|
||||
Timezone string `yaml:"timezone"`
|
||||
Environment string `yaml:"environment"`
|
||||
}
|
||||
|
||||
type LoggerConfig struct {
|
||||
Level string `yaml:"level"`
|
||||
Format string `yaml:"format"`
|
||||
Output string `yaml:"output"`
|
||||
Filename string `yaml:"filename"`
|
||||
MaxSize int `yaml:"max_size"`
|
||||
MaxAge int `yaml:"max_age"`
|
||||
MaxBackups int `yaml:"max_backups"`
|
||||
Compress bool `yaml:"compress"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
DBName string `yaml:"dbname"`
|
||||
SSLMode string `yaml:"sslmode"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Password string `yaml:"password"`
|
||||
Database int `yaml:"database"`
|
||||
PoolSize int `yaml:"pool_size"`
|
||||
MinIdleConns int `yaml:"min_idle_conns"`
|
||||
MaxRetries int `yaml:"max_retries"`
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
Secret string `yaml:"secret"`
|
||||
ExpireHours int `yaml:"expire_hours"`
|
||||
Issuer string `yaml:"issuer"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
App AppConfig `yaml:"app"`
|
||||
Logger LoggerConfig `yaml:"logger"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
Redis RedisConfig `yaml:"redis"`
|
||||
JWT JWTConfig `yaml:"jwt"`
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
data, err := os.ReadFile("config/app.yaml")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
cfg.setDefaults()
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) setDefaults() {
|
||||
if c.App.Host == "" {
|
||||
c.App.Host = "0.0.0.0"
|
||||
}
|
||||
if c.App.Port == 0 {
|
||||
c.App.Port = 8080
|
||||
}
|
||||
if c.App.Version == "" {
|
||||
c.App.Version = "0.1.0"
|
||||
}
|
||||
if c.App.Timezone == "" {
|
||||
c.App.Timezone = "UTC"
|
||||
}
|
||||
if c.App.Environment == "" {
|
||||
c.App.Environment = "development"
|
||||
}
|
||||
|
||||
if c.Logger.Level == "" {
|
||||
c.Logger.Level = "info"
|
||||
}
|
||||
if c.Logger.Format == "" {
|
||||
c.Logger.Format = "console"
|
||||
}
|
||||
if c.Logger.Output == "" {
|
||||
c.Logger.Output = "stdout"
|
||||
}
|
||||
if c.Logger.MaxSize == 0 {
|
||||
c.Logger.MaxSize = 100
|
||||
}
|
||||
if c.Logger.MaxAge == 0 {
|
||||
c.Logger.MaxAge = 30
|
||||
}
|
||||
if c.Logger.MaxBackups == 0 {
|
||||
c.Logger.MaxBackups = 3
|
||||
}
|
||||
|
||||
if c.Database.SSLMode == "" {
|
||||
c.Database.SSLMode = "disable"
|
||||
}
|
||||
if c.Database.MaxIdleConns == 0 {
|
||||
c.Database.MaxIdleConns = 10
|
||||
}
|
||||
if c.Database.MaxOpenConns == 0 {
|
||||
c.Database.MaxOpenConns = 100
|
||||
}
|
||||
if c.Database.ConnMaxLifetime == 0 {
|
||||
c.Database.ConnMaxLifetime = 60
|
||||
}
|
||||
|
||||
if c.Redis.Host == "" {
|
||||
c.Redis.Host = "localhost"
|
||||
}
|
||||
if c.Redis.Port == 0 {
|
||||
c.Redis.Port = 6379
|
||||
}
|
||||
if c.Redis.Database == 0 {
|
||||
c.Redis.Database = 0
|
||||
}
|
||||
if c.Redis.PoolSize == 0 {
|
||||
c.Redis.PoolSize = 10
|
||||
}
|
||||
if c.Redis.MinIdleConns == 0 {
|
||||
c.Redis.MinIdleConns = 5
|
||||
}
|
||||
if c.Redis.MaxRetries == 0 {
|
||||
c.Redis.MaxRetries = 3
|
||||
}
|
||||
|
||||
if c.JWT.Secret == "" {
|
||||
c.JWT.Secret = "change-this-secret-key-in-production"
|
||||
}
|
||||
if c.JWT.ExpireHours == 0 {
|
||||
c.JWT.ExpireHours = 24
|
||||
}
|
||||
if c.JWT.Issuer == "" {
|
||||
c.JWT.Issuer = "HeTianXia"
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) GetAddr() string {
|
||||
return fmt.Sprintf("%s:%d", c.App.Host, c.App.Port)
|
||||
}
|
||||
|
||||
func (c *Config) GetDSN() string {
|
||||
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
||||
c.Database.Host,
|
||||
c.Database.Port,
|
||||
c.Database.Username,
|
||||
c.Database.Password,
|
||||
c.Database.DBName,
|
||||
c.Database.SSLMode,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user