1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package config
-
- import (
- "io/ioutil"
- "log"
- "path"
- "strings"
-
- "github.com/Unknwon/i18n"
- "github.com/gin-gonic/gin"
- "github.com/joho/godotenv"
- "github.com/sirupsen/logrus"
- "github.com/spf13/viper"
- )
-
- // global config
- var (
- C appConfig
- )
-
- type appConfig struct {
- App struct {
- Debug bool `mapstructure:"debug"`
- Secret string `mapstructure:"secret"`
- } `mapstructure:"app"`
- DB struct {
- Host string `mapstructure:"host"`
- User string `mapstructure:"user"`
- Password string `mapstructure:"password"`
- Name string `mapstructure:"name"`
- MaxIdleConnections int `mapstructure:"max_idle_connections"`
- MaxOpenConnections int `mapstructure:"max_open_connections"`
- } `mapstructure:"db"`
- Redis struct {
- Address string `mapstructure:"address"`
- Password string `mapstructure:"password"`
- PoolSize int `mapstructure:"pool_size"`
- } `mapstructure:"redis"`
- }
-
- func init() {
- // 去掉烦人的gin提示,在http模块中会根据需要打开
- gin.SetMode(gin.ReleaseMode)
-
- // load .env file for testing
- godotenv.Load()
-
- // app
- viper.SetDefault("app.debug", true)
- viper.SetDefault("app.secret", "123456")
-
- // db
- viper.SetDefault("db.host", "localhost:3306")
- viper.SetDefault("db.user", "user")
- viper.SetDefault("db.password", "password")
- viper.SetDefault("db.name", "name")
- viper.SetDefault("db.max_idle_connections", 20)
- viper.SetDefault("db.max_open_connections", 50)
-
- // redis
- viper.SetDefault("redis.address", "localhost")
- viper.SetDefault("redis.password", "")
- viper.SetDefault("redis.pool_size", 10)
-
- // bind env
- viper.SetEnvPrefix("skeleton")
- viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
- viper.AutomaticEnv()
-
- // unmarshal config to struct
- if err := viper.Unmarshal(&C); err != nil {
- log.Fatalf("viper.Unmarshal error: %v\n", err)
- }
-
- // set logrus debug mode
- if C.App.Debug {
- logrus.SetLevel(logrus.DebugLevel)
- }
-
- // load language file for i18n
- files, err := ioutil.ReadDir("languages")
- if err == nil {
- for _, file := range files {
- if err := i18n.SetMessage(strings.TrimSuffix(file.Name(), path.Ext(file.Name())), "languages/"+file.Name()); err != nil {
- log.Fatalf("i18n.SetMessage error: %v\n", err)
- }
- }
-
- i18n.SetDefaultLang("en-US")
- }
- }
|