http urls monitor.

config.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // global config
  14. var (
  15. C appConfig
  16. )
  17. type appConfig struct {
  18. App struct {
  19. Debug bool `mapstructure:"debug"`
  20. Secret string `mapstructure:"secret"`
  21. } `mapstructure:"app"`
  22. DB struct {
  23. Host string `mapstructure:"host"`
  24. User string `mapstructure:"user"`
  25. Password string `mapstructure:"password"`
  26. Name string `mapstructure:"name"`
  27. MaxIdleConnections int `mapstructure:"max_idle_connections"`
  28. MaxOpenConnections int `mapstructure:"max_open_connections"`
  29. } `mapstructure:"db"`
  30. Redis struct {
  31. Address string `mapstructure:"address"`
  32. Password string `mapstructure:"password"`
  33. PoolSize int `mapstructure:"pool_size"`
  34. } `mapstructure:"redis"`
  35. }
  36. func init() {
  37. // 去掉烦人的gin提示,在http模块中会根据需要打开
  38. gin.SetMode(gin.ReleaseMode)
  39. // load .env file for testing
  40. godotenv.Load()
  41. // app
  42. viper.SetDefault("app.debug", true)
  43. viper.SetDefault("app.secret", "123456")
  44. // db
  45. viper.SetDefault("db.host", "localhost:3306")
  46. viper.SetDefault("db.user", "user")
  47. viper.SetDefault("db.password", "password")
  48. viper.SetDefault("db.name", "name")
  49. viper.SetDefault("db.max_idle_connections", 20)
  50. viper.SetDefault("db.max_open_connections", 50)
  51. // redis
  52. viper.SetDefault("redis.address", "localhost")
  53. viper.SetDefault("redis.password", "")
  54. viper.SetDefault("redis.pool_size", 10)
  55. // bind env
  56. viper.SetEnvPrefix("skeleton")
  57. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  58. viper.AutomaticEnv()
  59. // unmarshal config to struct
  60. if err := viper.Unmarshal(&C); err != nil {
  61. log.Fatalf("viper.Unmarshal error: %v\n", err)
  62. }
  63. // set logrus debug mode
  64. if C.App.Debug {
  65. logrus.SetLevel(logrus.DebugLevel)
  66. }
  67. // load language file for i18n
  68. files, err := ioutil.ReadDir("languages")
  69. if err == nil {
  70. for _, file := range files {
  71. if err := i18n.SetMessage(strings.TrimSuffix(file.Name(), path.Ext(file.Name())), "languages/"+file.Name()); err != nil {
  72. log.Fatalf("i18n.SetMessage error: %v\n", err)
  73. }
  74. }
  75. i18n.SetDefaultLang("en-US")
  76. }
  77. }