http urls monitor.

config.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Notice struct {
  23. SMTP struct {
  24. Host string `mapstructure:"host"`
  25. Port string `mapstructure:"port"`
  26. User string `mapstructure:"user"`
  27. Password string `mapstructure:"password"`
  28. } `mapstructure:"smtp"`
  29. URLs []string `mapstructure:"urls"`
  30. } `mapstructure:"notice"`
  31. }
  32. func init() {
  33. // 去掉烦人的gin提示,在http模块中会根据需要打开
  34. gin.SetMode(gin.ReleaseMode)
  35. // load .env file for testing
  36. godotenv.Load()
  37. // app
  38. viper.SetDefault("app.debug", true)
  39. viper.SetDefault("app.secret", "123456")
  40. // smtp
  41. viper.SetDefault("notice.smtp.host", "host")
  42. viper.SetDefault("notice.smtp.port", "port")
  43. viper.SetDefault("notice.smtp.user", "user")
  44. viper.SetDefault("notice.smtp.password", "password")
  45. // bind env
  46. viper.SetEnvPrefix("monitor")
  47. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  48. viper.AutomaticEnv()
  49. // unmarshal config to struct
  50. if err := viper.Unmarshal(&C); err != nil {
  51. log.Fatalf("viper.Unmarshal error: %v\n", err)
  52. }
  53. // set logrus debug mode
  54. if C.App.Debug {
  55. logrus.SetLevel(logrus.DebugLevel)
  56. }
  57. // load language file for i18n
  58. files, err := ioutil.ReadDir("languages")
  59. if err == nil {
  60. for _, file := range files {
  61. if err := i18n.SetMessage(strings.TrimSuffix(file.Name(), path.Ext(file.Name())), "languages/"+file.Name()); err != nil {
  62. log.Fatalf("i18n.SetMessage error: %v\n", err)
  63. }
  64. }
  65. i18n.SetDefaultLang("en-US")
  66. }
  67. }