config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package service
  2. import (
  3. "os"
  4. "github.com/BurntSushi/toml"
  5. )
  6. type Config struct {
  7. WebAddr string `toml:"WebAddr"`
  8. WebIndex string `toml: "WebIndex"`
  9. REFRESH_TOKENS string `toml: "REFRESH_TOKENS"`
  10. PUSHPLUS_TOKEN string `toml: "PUSHPLUS_TOKEN"`
  11. EmailUser string `toml: "EmailUser"`
  12. EmailPwd string `toml: "EmailPwd"`
  13. EmailSmtp string `toml: "EmailSmtp"`
  14. EmailTls bool `toml: "EmailTls"`
  15. Username string `toml:"Username"`
  16. Password string `toml:"Password"`
  17. }
  18. var config *Config
  19. func LoadConfig(path string) *Config {
  20. conf := &Config{}
  21. _, err := toml.DecodeFile(path, conf)
  22. if err != nil {
  23. panic(err)
  24. }
  25. config = conf
  26. logger.Info(config)
  27. return config
  28. }
  29. // todo
  30. func saveConfig() {
  31. // sava config to file
  32. conf := &Config{}
  33. conf.WebAddr = config.WebAddr
  34. conf.WebIndex = config.WebIndex
  35. conf.REFRESH_TOKENS = config.REFRESH_TOKENS
  36. conf.PUSHPLUS_TOKEN = config.PUSHPLUS_TOKEN
  37. conf.EmailUser = config.EmailUser
  38. conf.EmailPwd = config.EmailPwd
  39. conf.EmailSmtp = config.EmailSmtp
  40. conf.EmailTls = config.EmailTls
  41. conf.Username = config.Username
  42. conf.Password = config.Password
  43. // save to file config.toml
  44. file, err := os.Create("config.toml")
  45. if err != nil {
  46. panic(err)
  47. }
  48. defer file.Close()
  49. encoder := toml.NewEncoder(file)
  50. err = encoder.Encode(conf)
  51. if err != nil {
  52. panic(err)
  53. }
  54. logger.Info("config saved to file config.toml")
  55. }