24ca379bdbdec3e547d0c01c3d41e93d2c35df4c.svn-base 3.0 KB

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