110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package conf
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
conf *Config
|
|
once sync.Once
|
|
)
|
|
|
|
type Config struct {
|
|
Env string
|
|
|
|
Server ServerConfig `mapstructure:"server" yaml:"server"`
|
|
JWT JwtConfig `mapstructure:"jwt" yaml:"jwt"`
|
|
Snowflake SnowflakeConfig `mapstructure:"snowflake" yaml:"snowflake"`
|
|
Pgsql PgsqlConfig `mapstructure:"pgsql" yaml:"pgsql"`
|
|
Log LogConfig `mapstructure:"log" yaml:"log"`
|
|
Wx WxConfig `mapstructure:"wx" yaml:"wx"`
|
|
Redis RedisConfig `mapstructure:"redis" yaml:"redis"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string `mapstructure:"port" yaml:"port"`
|
|
BaseUrl string `mapstructure:"base_url" yaml:"base_url"` // 下划线
|
|
}
|
|
|
|
type JwtConfig struct {
|
|
Secret string `mapstructure:"secret" yaml:"secret"`
|
|
AccessExpiry time.Duration `mapstructure:"access_expiry" yaml:"access_expiry"`
|
|
}
|
|
|
|
type SnowflakeConfig struct {
|
|
DatacenterID uint16 `mapstructure:"datacenter_id" yaml:"datacenter_id"`
|
|
WorkerID uint16 `mapstructure:"worker_id" yaml:"worker_id"`
|
|
}
|
|
|
|
type PgsqlConfig struct {
|
|
Host string `mapstructure:"host" yaml:"host"`
|
|
Port int `mapstructure:"port" yaml:"port"`
|
|
User string `mapstructure:"user" yaml:"user"`
|
|
Password string `mapstructure:"password" yaml:"password"`
|
|
Dbname string `mapstructure:"dbname" yaml:"dbname"`
|
|
SllMode bool `mapstructure:"sll_mode" yaml:"sll_mode"`
|
|
TimeZone string `mapstructure:"timezone" yaml:"timezone"`
|
|
MaxOpenConns int32 `mapstructure:"max_open_conns" yaml:"max_open_conns"`
|
|
MaxIdleConns int32 `mapstructure:"max_idle_conns" yaml:"max_idle_conns"`
|
|
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime" yaml:"conn_max_lifetime"`
|
|
}
|
|
|
|
type LogConfig struct {
|
|
Level string `mapstructure:"level" yaml:"level"`
|
|
Dir string `mapstructure:"dir" yaml:"dir"`
|
|
MaxSize int `mapstructure:"max_size" yaml:"max_size"`
|
|
MaxBackups int `mapstructure:"max_backups" yaml:"max_backups"`
|
|
MaxAge int `mapstructure:"max_age" yaml:"max_age"`
|
|
Compress bool `mapstructure:"compress" yaml:"compress"`
|
|
}
|
|
|
|
type WxConfig struct {
|
|
AppId string `mapstructure:"app_id" yaml:"app_id"`
|
|
AppSecret string `mapstructure:"app_secret" yaml:"app_secret"`
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Host string `mapstructure:"host" yaml:"host"`
|
|
Port int `mapstructure:"port" yaml:"port"`
|
|
Db int `mapstructure:"db" yaml:"db"`
|
|
Password string `mapstructure:"password" yaml:"password"`
|
|
}
|
|
|
|
func GetConf() *Config {
|
|
once.Do(initConf)
|
|
return conf
|
|
}
|
|
|
|
func initConf() {
|
|
prefix := "conf"
|
|
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
|
|
|
|
v := viper.New()
|
|
v.SetConfigFile(confFileRelPath)
|
|
v.SetConfigType("yaml")
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
conf = new(Config)
|
|
if err := v.Unmarshal(conf); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
conf.Env = GetEnv()
|
|
}
|
|
|
|
func GetEnv() string {
|
|
e := os.Getenv("GO_ENV")
|
|
if len(e) == 0 {
|
|
return "dev"
|
|
}
|
|
return e
|
|
}
|