Text to Speech Speech to Text

config.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package config
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "path"
  6. "strings"
  7. "github.com/Unknwon/i18n"
  8. "github.com/gin-gonic/gin"
  9. "github.com/joho/godotenv"
  10. "github.com/sirupsen/logrus"
  11. "github.com/spf13/viper"
  12. )
  13. var (
  14. C appConfig
  15. )
  16. type appConfig struct {
  17. App struct {
  18. Debug bool `mapstructure:"debug"`
  19. InChina bool `mapstructure:"in_china"`
  20. Secret string `mapstructure:"secret"`
  21. ApiHost string `mapstructure:"api_host"`
  22. CNHost string `mapstructure:"api_cn_host"`
  23. HKHost string `mapstructure:"api_hk_host"`
  24. } `mapstructure:"app"`
  25. DB struct {
  26. Host string `mapstructure:"host"`
  27. User string `mapstructure:"user"`
  28. Password string `mapstructure:"password"`
  29. Name string `mapstructure:"name"`
  30. MaxIdleConnections int `mapstructure:"max_idle_connections"`
  31. MaxOpenConnections int `mapstructure:"max_open_connections"`
  32. } `mapstructure:"db"`
  33. OSS struct {
  34. AccessKey string `mapstructure:"access_key"`
  35. SecretKey string `mapstructure:"secret_key"`
  36. Bucket string `mapstructure:"bucket"`
  37. EndPoint string `mapstructure:"end_point"`
  38. HkBucket string `mapstructure:"hk_bucket"`
  39. HkEndPoint string `mapstructure:"hk_end_point"`
  40. } `mapstructure:"oss"`
  41. Redis struct {
  42. Address string `mapstructure:"address"`
  43. Password string `mapstructure:"password"`
  44. PoolSize int `mapstructure:"pool_size"`
  45. } `mapstructure:"redis"`
  46. MaxMind struct {
  47. UserId string `mapstructure:"user_id"`
  48. LicenseKey string `mapstructure:"license_key"`
  49. } `mapstructure:"maxmind"`
  50. }
  51. func init() {
  52. // 去掉烦人的gin提示,在http模块中会根据需要打开
  53. gin.SetMode(gin.ReleaseMode)
  54. // load .env file for testing
  55. godotenv.Load()
  56. //fmt.Println("Connect to redis error", err)
  57. // app
  58. viper.SetDefault("app.debug", true)
  59. viper.SetDefault("app.secret", "123456")
  60. viper.SetDefault("app.in_china", true)
  61. viper.SetDefault("app.api_host", "")
  62. viper.SetDefault("app.api_cn_host", "")
  63. viper.SetDefault("app.api_hk_host", "")
  64. // db
  65. viper.SetDefault("db.host", "localhost:3306")
  66. viper.SetDefault("db.user", "user")
  67. viper.SetDefault("db.password", "password")
  68. viper.SetDefault("db.name", "name")
  69. viper.SetDefault("db.max_idle_connections", 20)
  70. viper.SetDefault("db.max_open_connections", 50)
  71. // redis
  72. viper.SetDefault("redis.address", "127.0.0.1")
  73. viper.SetDefault("redis.password", "")
  74. viper.SetDefault("redis.pool_size", 10)
  75. // oss
  76. viper.SetDefault("oss.access_key", "")
  77. viper.SetDefault("oss.secret_key", "")
  78. viper.SetDefault("oss.bucket", "")
  79. viper.SetDefault("oss.end_point", "")
  80. viper.SetDefault("oss.hk_bucket", "")
  81. viper.SetDefault("oss.hk_end_point", "")
  82. // MaxMind
  83. viper.SetDefault("maxmind.user_id", "")
  84. viper.SetDefault("maxmind.license_key", "")
  85. // bind env
  86. viper.SetEnvPrefix("CorpusAI")
  87. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  88. viper.AutomaticEnv()
  89. // unmarshal config to struct
  90. if err := viper.Unmarshal(&C); err != nil {
  91. log.Fatalf("viper.Unmarshal error: %v\n", err)
  92. }
  93. // set run mode
  94. switch C.App.Debug {
  95. case true:
  96. logrus.SetLevel(logrus.DebugLevel)
  97. case false:
  98. gin.SetMode(gin.ReleaseMode)
  99. }
  100. // load language file for i18n
  101. files, err := ioutil.ReadDir("languages")
  102. if err == nil {
  103. for _, file := range files {
  104. if err := i18n.SetMessage(strings.TrimSuffix(file.Name(), path.Ext(file.Name())), "languages/"+file.Name()); err != nil {
  105. log.Fatalf("i18n.SetMessage error: %v\n", err)
  106. }
  107. }
  108. i18n.SetDefaultLang("en-US")
  109. }
  110. }