setting.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. UDForwardingSvc 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. }
  78. type SQlserver struct {
  79. Host string
  80. User string
  81. Password string
  82. Name string
  83. }
  84. type Yyn struct {
  85. FarmId string
  86. ProductKey string
  87. DeviceName string
  88. DeviceSecret string
  89. HeartBeat string
  90. }
  91. func Setup() {
  92. var err error
  93. workDir := os.Getenv("GO_WORK_DIR_TMR")
  94. if workDir == "" {
  95. CurrentPath, err = GetCurrentPath()
  96. } else {
  97. CurrentPath = workDir
  98. }
  99. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  100. switch appEnv {
  101. case "test":
  102. cfg, err = ini.Load(CurrentPath + "conf/app-test.ini")
  103. case "development":
  104. cfg, err = ini.Load(CurrentPath + "conf/app-develop.ini")
  105. default:
  106. cfg, err = ini.Load(CurrentPath + "conf/app.ini")
  107. }
  108. if err != nil {
  109. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  110. }
  111. SnowIds, err = NewIdWorker(1)
  112. if err != nil {
  113. log.Fatalf("setting.Setup, fail to create SnowIds: %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. mapTo("sqlserver", SQlserverSetting)
  123. mapTo("yyn", YynserverSetting)
  124. AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
  125. ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
  126. ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second
  127. ServerSetting.CrtPath = CurrentPath + ServerSetting.CrtPath
  128. ServerSetting.KeyPath = CurrentPath + ServerSetting.KeyPath
  129. }
  130. func GetCurrentPath() (string, error) {
  131. file, err := exec.LookPath(os.Args[0])
  132. if err != nil {
  133. return "", err
  134. }
  135. path, err := filepath.Abs(file)
  136. if err != nil {
  137. return "", err
  138. }
  139. if runtime.GOOS == "windows" {
  140. path = strings.Replace(path, "\\", "/", -1)
  141. }
  142. i := strings.LastIndex(path, "/")
  143. if i < 0 {
  144. return "", errors.New(`Can't find "/" or "\".`)
  145. }
  146. return path[0 : i+1], nil
  147. }
  148. func mapTo(section string, v interface{}) {
  149. err := cfg.Section(section).MapTo(v)
  150. if err != nil {
  151. log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
  152. }
  153. }