setting.go 3.7 KB

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