4db678964cee13d15cd4fb591b66f8fd1d5b3acf.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  55. var ServerSetting = &Server{}
  56. var CommSetting = &serial.OpenOptions{}
  57. var SnowIds *util.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 =util.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. }