package config import ( "cls-server/pkg/logger" "github.com/spf13/viper" ) var log = logger.NewLogger("cls") // AppConfig 应用配置 type AppConfig struct { ClickhouseConfig ClickhouseConfig `mapstructure:"clickhouse"` ServerConfig ServerConfig `mapstructure:"server"` LogConfig LogConfig `mapstructure:"log"` JwtConfig JwtConfig `mapstructure:"jwt"` MysqlConfig MysqlConfig `mapstructure:"mysql"` TagsConfig TagsConfig `mapstructure:"tags"` RedisConfig RedisConfig `mapstructure:"redis"` } type TagsConfig struct { TagsTable string `mapstructure:"tags_table"` PhoneKey string `mapstructure:"phone_key"` } // ServerConfig Http服务器配置 type ServerConfig struct { Mode string `mapstructure:"mode"` // 可选 dev,prod, 默认 dev Port int `mapstructure:"port"` } // LogConfig 日志配置 type LogConfig struct { Level string `mapstructure:"level"` // 根日志级别 Loggers map[string]string `mapstructure:"loggers"` // 模块日志级别 } // JwtConfig jwt配置 type JwtConfig struct { Realm string `mapstructure:"realm"` Secret string `mapstructure:"secret"` } // MysqlConfig mysql配置 type MysqlConfig struct { Address string `mapstructure:"address"` Driver string `mapstructure:"driver"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` Database string `mapstructure:"database"` DatabaseCls string `mapstructure:"database_cls"` Params map[string]string `mapstructure:"params"` ShowSql bool `mapstructure:"showsql"` } // ClickhouseConfig clickhouse配置 type ClickhouseConfig struct { Address []string `mapstructure:"address"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` Database string `mapstructure:"database"` } type RedisConfig struct { // default stand-alone Mode string `mapstructure:"mode"` Addr string `mapstructure:"addr"` // cluster Addrs []string `mapstructure:"addrs"` // sentinel MasterName string `mapstructure:"master_name"` SentinelAddrs []string `mapstructure:"sentinel_addrs"` SentinelUsername string `mapstructure:"sentinel_username"` SentinelPassword string `mapstructure:"sentinel_password"` // common Username string `mapstructure:"username"` Password string `mapstructure:"password"` DB int `mapstructure:"db"` } func NewAppConfig() *AppConfig { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath("./config") if err := viper.ReadInConfig(); err != nil { log.Fatalf("fatal error config file: %w", err) } viper.AutomaticEnv() log.Info("Use config file: " + viper.ConfigFileUsed()) config := &AppConfig{} if err := viper.Unmarshal(config); err != nil { log.Fatal(err) } return config }