setting.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package setting
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/pkg/errors"
  12. "github.com/go-ini/ini"
  13. "github.com/jacobsa/go-serial/serial"
  14. )
  15. var AppSetting = &App{}
  16. var ServerSetting = &Server{}
  17. var CommSetting = &serial.OpenOptions{}
  18. type App struct {
  19. JwtSecret string
  20. PageSize int
  21. PrefixUrl string
  22. RuntimeRootPath string
  23. FileSavePath string
  24. FileMaxSize int // 普通文件上传的限制大小,单位byte, 最大单位1GB
  25. FileAllowType []string // 允许上传的文件后缀名
  26. ImageSavePath string
  27. ImageMaxSize int
  28. ImageAllowExts []string
  29. ThumbnailSavePath string
  30. ThumbnailMaxWidth int
  31. ThumbnailMaxHeight int
  32. ExportSavePath string
  33. QrCodeSavePath string
  34. FontSavePath string
  35. LogSavePath string
  36. LogSaveName string
  37. LogFileExt string
  38. TimeFormat string
  39. }
  40. type Server struct {
  41. Env string
  42. RunMode string
  43. HttpPort int
  44. ReadTimeout time.Duration
  45. WriteTimeout time.Duration
  46. NoAuth int
  47. Mdns_servicename string
  48. Mdns_serviceport int
  49. HttpsPort int
  50. Https int
  51. CrtPath string
  52. KeyPath string
  53. MailStart string
  54. CronFormula string
  55. WXworkStart string
  56. WXworkAgentid string
  57. WXworkSecret string
  58. WXworkCorpid string
  59. ServerName string
  60. DisplayName string
  61. Description string
  62. EasName string
  63. EasPassword string
  64. EasUrl string
  65. EasUrlTem string
  66. }
  67. type Database struct {
  68. Type string
  69. User string
  70. Password string
  71. Host string
  72. Name string
  73. ShowXormlog bool
  74. ShowGetSqllog bool
  75. CacheApiSql bool
  76. TablePrefix string
  77. Dsn string
  78. }
  79. var DatabaseSetting = &Database{}
  80. var cfg *ini.File
  81. var CurrentPath string
  82. func init() {
  83. flag.StringVar(&CurrentPath, "iconf", "", "default config path")
  84. }
  85. func GetCurrentPath() (string, error) {
  86. file, err := exec.LookPath(os.Args[0])
  87. if err != nil {
  88. return "", err
  89. }
  90. path, err := filepath.Abs(file)
  91. if err != nil {
  92. return "", err
  93. }
  94. if runtime.GOOS == "windows" {
  95. path = strings.Replace(path, "\\", "/", -1)
  96. }
  97. i := strings.LastIndex(path, "/")
  98. if i < 0 {
  99. return "", errors.New(`Can't find "/" or "\".`)
  100. }
  101. return string(path[0 : i+1]), nil
  102. }
  103. func Setup(dir string) {
  104. var err error
  105. if strings.Trim(dir, " ") == "" {
  106. CurrentPath, err = GetCurrentPath()
  107. } else {
  108. CurrentPath = dir
  109. }
  110. cfg, err = ini.Load(CurrentPath + "conf/app.ini")
  111. if err != nil {
  112. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %s", CurrentPath)
  113. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  114. }
  115. DatabaseSetting.ShowXormlog = false
  116. DatabaseSetting.ShowGetSqllog = false
  117. DatabaseSetting.CacheApiSql = false
  118. mapTo("app", AppSetting)
  119. mapTo("server", ServerSetting)
  120. mapTo("database", DatabaseSetting)
  121. mapTo("comm", CommSetting)
  122. AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
  123. ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
  124. ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second
  125. ServerSetting.CrtPath = CurrentPath + ServerSetting.CrtPath
  126. ServerSetting.KeyPath = CurrentPath + ServerSetting.KeyPath
  127. }
  128. func mapTo(section string, v interface{}) {
  129. err := cfg.Section(section).MapTo(v)
  130. if err != nil {
  131. log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
  132. }
  133. }