setting.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ServerName string
  50. DisplayName string
  51. Description string
  52. GRFD string
  53. GRFDURL string
  54. }
  55. var ServerSetting = &Server{}
  56. var CommSetting = &serial.OpenOptions{}
  57. var SnowIds *IdWorker
  58. type Database struct {
  59. Type string
  60. User string
  61. Password string
  62. Host string
  63. Name string
  64. ShowXormlog bool
  65. ShowGetSqllog bool
  66. CacheApiSql bool
  67. TablePrefix string
  68. }
  69. var DatabaseSetting = &Database{}
  70. var cfg *ini.File
  71. var CurrentPath string
  72. func GetCurrentPath() (string, error) {
  73. file, err := exec.LookPath(os.Args[0])
  74. if err != nil {
  75. return "", err
  76. }
  77. path, err := filepath.Abs(file)
  78. if err != nil {
  79. return "", err
  80. }
  81. //fmt.Println("path111:", path)
  82. if runtime.GOOS == "windows" {
  83. path = strings.Replace(path, "\\", "/", -1)
  84. }
  85. //fmt.Println("path222:", path)
  86. i := strings.LastIndex(path, "/")
  87. if i < 0 {
  88. return "", errors.New(`Can't find "/" or "\".`)
  89. }
  90. //fmt.Println("path333:", path)
  91. return string(path[0 : i+1]), nil
  92. }
  93. func Setup(dir string) {
  94. var err error
  95. if strings.Trim(dir, " ") == "" {
  96. CurrentPath, err = GetCurrentPath()
  97. } else {
  98. CurrentPath = dir
  99. }
  100. cfg, err = ini.Load(CurrentPath + "conf/app.ini")
  101. if err != nil {
  102. log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
  103. }
  104. SnowIds, err = NewIdWorker(1)
  105. if err != nil {
  106. log.Fatalf("setting.Setup, fail to create SnowIds: %v", err)
  107. }
  108. DatabaseSetting.ShowXormlog = false
  109. DatabaseSetting.ShowGetSqllog = false
  110. DatabaseSetting.CacheApiSql = false
  111. mapTo("app", AppSetting)
  112. mapTo("server", ServerSetting)
  113. mapTo("database", DatabaseSetting)
  114. mapTo("comm", CommSetting)
  115. AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
  116. ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
  117. ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second
  118. ServerSetting.CrtPath = CurrentPath + ServerSetting.CrtPath
  119. ServerSetting.KeyPath = CurrentPath + ServerSetting.KeyPath
  120. }
  121. func mapTo(section string, v interface{}) {
  122. err := cfg.Section(section).MapTo(v)
  123. if err != nil {
  124. log.Fatalf("Cfg.MapTo RedisSetting err: %v", err)
  125. }
  126. }