config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package config
  2. import (
  3. "embed"
  4. "encoding/json"
  5. "io/fs"
  6. "os"
  7. "strings"
  8. )
  9. var IsInit bool
  10. type Config struct {
  11. LogLevel string `json:"logLevel"` // 日志级别
  12. Domain string `json:"domain"`
  13. WebDomain string `json:"webDomain"`
  14. DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
  15. SSLType string `json:"sslType"` // 0表示自动生成证书,1表示用户上传证书
  16. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  17. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  18. DbDSN string `json:"dbDSN"`
  19. DbType string `json:"dbType"`
  20. WeChatPushAppId string `json:"weChatPushAppId"`
  21. WeChatPushSecret string `json:"weChatPushSecret"`
  22. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  23. WeChatPushUserId string `json:"weChatPushUserId"`
  24. TgBotToken string `json:"tgBotToken"`
  25. TgChatId string `json:"tgChatId"`
  26. IsInit bool `json:"isInit"`
  27. HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
  28. Tables map[string]string `json:"-"`
  29. TablesInitData map[string]string `json:"-"`
  30. }
  31. //go:embed tables/*
  32. var tableConfig embed.FS
  33. const Version = "2.1.2"
  34. const DBTypeMySQL = "mysql"
  35. const DBTypeSQLite = "sqlite"
  36. const SSLTypeAuto = "0" //自动生成证书
  37. const SSLTypeUser = "1" //用户上传证书
  38. var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
  39. var Instance *Config
  40. func Init() {
  41. var cfgData []byte
  42. var err error
  43. args := os.Args
  44. if len(args) >= 2 && args[len(args)-1] == "dev" {
  45. cfgData, err = os.ReadFile("./config/config.dev.json")
  46. if err != nil {
  47. return
  48. }
  49. } else {
  50. cfgData, err = os.ReadFile("./config/config.json")
  51. if err != nil {
  52. return
  53. }
  54. }
  55. err = json.Unmarshal(cfgData, &Instance)
  56. if err != nil {
  57. return
  58. }
  59. // 读取表设置
  60. Instance.Tables = map[string]string{}
  61. Instance.TablesInitData = map[string]string{}
  62. root := "tables/mysql"
  63. if Instance.DbType == DBTypeSQLite {
  64. root = "tables/sqlite"
  65. }
  66. err = fs.WalkDir(tableConfig, root, func(path string, info fs.DirEntry, err error) error {
  67. if !info.IsDir() && strings.HasSuffix(info.Name(), ".sql") {
  68. tableName := strings.ReplaceAll(info.Name(), ".sql", "")
  69. i, e := tableConfig.ReadFile(path)
  70. if e != nil {
  71. panic(e)
  72. }
  73. if strings.Contains(path, "data") {
  74. Instance.TablesInitData[tableName] = string(i)
  75. } else {
  76. Instance.Tables[tableName] = string(i)
  77. }
  78. }
  79. return nil
  80. })
  81. if err != nil {
  82. panic(err)
  83. }
  84. if Instance.Domain != "" && Instance.IsInit {
  85. IsInit = true
  86. }
  87. }