http urls monitor.

config.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Monitor struct {
  23. Timeout int64 `mapstructure:"timeout"`
  24. URLs string `mapstructure:"urls"` // url_1;url_2
  25. }
  26. Notice struct {
  27. SMTP struct {
  28. Host string `mapstructure:"host"`
  29. Port string `mapstructure:"port"`
  30. User string `mapstructure:"user"`
  31. Password string `mapstructure:"password"`
  32. } `mapstructure:"smtp"`
  33. Emails string `mapstructure:"emails"`
  34. } `mapstructure:"notice"`
  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. // smtp
  45. viper.SetDefault("notice.smtp.host", "host")
  46. viper.SetDefault("notice.smtp.port", "port")
  47. viper.SetDefault("notice.smtp.user", "user")
  48. viper.SetDefault("notice.smtp.password", "password")
  49. viper.SetDefault("notice.emails", "")
  50. // monitor
  51. viper.SetDefault("monitor.timeout", 500)
  52. viper.SetDefault("monitor.urls", "")
  53. // bind env
  54. viper.SetEnvPrefix("monitor")
  55. viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  56. viper.AutomaticEnv()
  57. // unmarshal config to struct
  58. if err := viper.Unmarshal(&C); err != nil {
  59. log.Fatalf("viper.Unmarshal error: %v\n", err)
  60. }
  61. // set logrus debug mode
  62. if C.App.Debug {
  63. logrus.SetLevel(logrus.DebugLevel)
  64. }
  65. // load language file for i18n
  66. files, err := ioutil.ReadDir("languages")
  67. if err == nil {
  68. for _, file := range files {
  69. if err := i18n.SetMessage(strings.TrimSuffix(file.Name(), path.Ext(file.Name())), "languages/"+file.Name()); err != nil {
  70. log.Fatalf("i18n.SetMessage error: %v\n", err)
  71. }
  72. }
  73. i18n.SetDefaultLang("en-US")
  74. }
  75. }