setting.go 3.0 KB

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