setting.go 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package setting
  2. import (
  3. "gopkg.in/ini.v1"
  4. "log"
  5. "time"
  6. )
  7. type App struct {
  8. PrefixUrl string
  9. RuntimeRootPath string
  10. LogSavePath string
  11. LogSaveName string
  12. LogFileExt string
  13. TimeFormat string
  14. }
  15. var AppSetting = &App{}
  16. // 应用程序配置
  17. type Server struct {
  18. RunMode string
  19. HttpPort int
  20. ReadTimeout time.Duration
  21. WriteTimeout time.Duration
  22. }
  23. var ServerSetting = &Server{}
  24. // Mssql数据库链接
  25. type Database struct {
  26. Server string
  27. UserId string
  28. Password string
  29. Port int
  30. DataBase string
  31. Encrypt string
  32. }
  33. var DatabaseSetting = &Database{}
  34. var cfg *ini.File
  35. func Setup() {
  36. var err error
  37. cfg, err = ini.Load("conf/app.ini")
  38. if err != nil {
  39. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  40. }
  41. mapTo("app", AppSetting)
  42. mapTo("server", ServerSetting)
  43. mapTo("database", DatabaseSetting)
  44. }
  45. func mapTo(section string, v interface{}) {
  46. err := cfg.Section(section).MapTo(v)
  47. if err != nil {
  48. log.Fatalf("Cfg.MapTo %s err: %v", section, err)
  49. }
  50. }