setting.go 3.1 KB

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