123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 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"
- )
-
- var (
- C appConfig
- )
-
- type appConfig struct {
- App struct {
- Debug bool `mapstructure:"debug"`
- InChina bool `mapstructure:"in_china"`
- Secret string `mapstructure:"secret"`
- ApiHost string `mapstructure:"api_host"`
- CNHost string `mapstructure:"api_cn_host"`
- HKHost string `mapstructure:"api_hk_host"`
- } `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"`
- OSS struct {
- AccessKey string `mapstructure:"access_key"`
- SecretKey string `mapstructure:"secret_key"`
- Bucket string `mapstructure:"bucket"`
- EndPoint string `mapstructure:"end_point"`
- HkBucket string `mapstructure:"hk_bucket"`
- HkEndPoint string `mapstructure:"hk_end_point"`
- } `mapstructure:"oss"`
- Redis struct {
- Address string `mapstructure:"address"`
- Password string `mapstructure:"password"`
- PoolSize int `mapstructure:"pool_size"`
- } `mapstructure:"redis"`
-
- MaxMind struct {
- UserId string `mapstructure:"user_id"`
- LicenseKey string `mapstructure:"license_key"`
- } `mapstructure:"maxmind"`
- }
-
- func init() {
-
- // 去掉烦人的gin提示,在http模块中会根据需要打开
- gin.SetMode(gin.ReleaseMode)
-
- // load .env file for testing
- godotenv.Load()
-
- //fmt.Println("Connect to redis error", err)
- // app
- viper.SetDefault("app.debug", true)
- viper.SetDefault("app.secret", "123456")
-
- viper.SetDefault("app.in_china", true)
- viper.SetDefault("app.api_host", "")
- viper.SetDefault("app.api_cn_host", "")
- viper.SetDefault("app.api_hk_host", "")
-
- // 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", "127.0.0.1")
- viper.SetDefault("redis.password", "")
- viper.SetDefault("redis.pool_size", 10)
-
- // oss
- viper.SetDefault("oss.access_key", "")
- viper.SetDefault("oss.secret_key", "")
- viper.SetDefault("oss.bucket", "")
- viper.SetDefault("oss.end_point", "")
- viper.SetDefault("oss.hk_bucket", "")
- viper.SetDefault("oss.hk_end_point", "")
-
- // MaxMind
- viper.SetDefault("maxmind.user_id", "")
- viper.SetDefault("maxmind.license_key", "")
-
- // bind env
- viper.SetEnvPrefix("CorpusAI")
- 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 run mode
- switch C.App.Debug {
- case true:
- logrus.SetLevel(logrus.DebugLevel)
- case false:
- gin.SetMode(gin.ReleaseMode)
- }
-
- // 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")
- }
- }
|