setting.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. Netcard string
  63. Broadcast int
  64. BroadcastAddr string
  65. UseAPI int
  66. SendRoute int64
  67. ControlComm int
  68. RelayDev int
  69. }
  70. var ServerSetting = &Server{}
  71. var CommSetting = &serial.OpenOptions{}
  72. var ControlCommSetting = &serial.OpenOptions{}
  73. type Database struct {
  74. Type string
  75. User string
  76. Password string
  77. Host string
  78. Name string
  79. ShowXormlog bool
  80. ShowGetSqllog bool
  81. Showline bool
  82. Showlog bool
  83. CacheApiSql bool
  84. TablePrefix string
  85. }
  86. var DatabaseSetting = &Database{}
  87. type Mqtt struct {
  88. Enable bool
  89. Host string
  90. Port int
  91. Nodevalue string
  92. Path string
  93. Topic_Up string
  94. Topic_Down string
  95. Qos int
  96. CleanSession bool
  97. SaslEnable bool
  98. Username string
  99. Saslpassword string
  100. TlsEnable bool
  101. Clientcert string
  102. Clientkey string
  103. Cacert string
  104. }
  105. var MqttSetting = &Mqtt{}
  106. var cfg *ini.File
  107. var CurrentPath string
  108. func GetCurrentPath() (string, error) {
  109. file, err := exec.LookPath(os.Args[0])
  110. if err != nil {
  111. return "", err
  112. }
  113. path, err := filepath.Abs(file)
  114. if err != nil {
  115. return "", err
  116. }
  117. //fmt.Println("path111:", path)
  118. if runtime.GOOS == "windows" {
  119. path = strings.Replace(path, "\\", "/", -1)
  120. }
  121. //fmt.Println("path222:", path)
  122. i := strings.LastIndex(path, "/")
  123. if i < 0 {
  124. return "", errors.New(`Can't find "/" or "\".`)
  125. }
  126. //fmt.Println("path333:", path)
  127. return string(path[0 : i+1]), nil
  128. }
  129. func Setup(dir string) {
  130. var err error
  131. if strings.Trim(dir, " ") == "" {
  132. CurrentPath, err = GetCurrentPath()
  133. } else {
  134. CurrentPath = dir
  135. }
  136. cfg, err = ini.Load(CurrentPath + "conf/app.ini")
  137. if err != nil {
  138. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  139. }
  140. DatabaseSetting.ShowXormlog = false
  141. DatabaseSetting.ShowGetSqllog = false
  142. DatabaseSetting.CacheApiSql = false
  143. mapTo("app", AppSetting)
  144. mapTo("server", ServerSetting)
  145. mapTo("database", DatabaseSetting)
  146. mapTo("comm", CommSetting)
  147. mapTo("Controlcomm", ControlCommSetting)
  148. AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
  149. ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
  150. ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second
  151. ServerSetting.CrtPath = CurrentPath + ServerSetting.CrtPath
  152. ServerSetting.KeyPath = CurrentPath + ServerSetting.KeyPath
  153. }
  154. func mapTo(section string, v interface{}) {
  155. err := cfg.Section(section).MapTo(v)
  156. if err != nil {
  157. log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
  158. }
  159. }