config.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package config
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. var IsInit bool
  7. type Config struct {
  8. LogLevel string `json:"logLevel"` // 日志级别
  9. Domain string `json:"domain"`
  10. Domains []string `json:"domains"` //多域名设置,把所有收信域名都填进去
  11. WebDomain string `json:"webDomain"`
  12. DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
  13. SSLType string `json:"sslType"` // 0表示自动生成证书,1表示用户上传证书
  14. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  15. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  16. DbDSN string `json:"dbDSN"`
  17. DbType string `json:"dbType"`
  18. HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
  19. SpamFilterLevel int `json:"spamFilterLevel"` //垃圾邮件过滤级别,0不过滤、1 spf dkim 校验均失败时过滤,2 spf校验不通过时过滤
  20. HttpPort int `json:"httpPort"` //http服务端口设置,默认80
  21. HttpsPort int `json:"httpsPort"` //https服务端口,默认443
  22. WeChatPushAppId string `json:"weChatPushAppId"`
  23. WeChatPushSecret string `json:"weChatPushSecret"`
  24. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  25. WeChatPushUserId string `json:"weChatPushUserId"`
  26. TgBotToken string `json:"tgBotToken"`
  27. TgChatId string `json:"tgChatId"`
  28. IsInit bool `json:"isInit"`
  29. WebPushUrl string `json:"webPushUrl"`
  30. WebPushToken string `json:"webPushToken"`
  31. Tables map[string]string `json:"-"`
  32. TablesInitData map[string]string `json:"-"`
  33. setupPort int // 初始化阶段端口
  34. }
  35. func (c *Config) GetSetupPort() int {
  36. return c.setupPort
  37. }
  38. func (c *Config) SetSetupPort(setupPort int) {
  39. c.setupPort = setupPort
  40. }
  41. const DBTypeMySQL = "mysql"
  42. const DBTypeSQLite = "sqlite"
  43. const SSLTypeAuto = "0" //自动生成证书
  44. const SSLTypeUser = "1" //用户上传证书
  45. var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
  46. var Instance *Config = &Config{}
  47. func Init() {
  48. var cfgData []byte
  49. var err error
  50. args := os.Args
  51. if len(args) >= 2 && args[len(args)-1] == "dev" {
  52. cfgData, err = os.ReadFile("./config/config.dev.json")
  53. if err != nil {
  54. return
  55. }
  56. } else {
  57. cfgData, err = os.ReadFile("./config/config.json")
  58. if err != nil {
  59. return
  60. }
  61. }
  62. err = json.Unmarshal(cfgData, &Instance)
  63. if err != nil {
  64. return
  65. }
  66. if len(Instance.Domains) == 0 && Instance.Domain != "" {
  67. Instance.Domains = []string{Instance.Domain}
  68. }
  69. if Instance.Domain != "" && Instance.IsInit {
  70. IsInit = true
  71. }
  72. }