config.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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表示自动生成证书,HTTP挑战模式,1表示用户上传证书,2表示自动-DNS挑战模式
  14. DomainServiceName string `json:"domainServerName"` // 域名服务商名称
  15. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  16. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  17. DbDSN string `json:"dbDSN"`
  18. DbType string `json:"dbType"`
  19. HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
  20. SpamFilterLevel int `json:"spamFilterLevel"` //垃圾邮件过滤级别,0不过滤、1 spf dkim 校验均失败时过滤,2 spf校验不通过时过滤
  21. HttpPort int `json:"httpPort"` //http服务端口设置,默认80
  22. HttpsPort int `json:"httpsPort"` //https服务端口,默认443
  23. WeChatPushAppId string `json:"weChatPushAppId"`
  24. WeChatPushSecret string `json:"weChatPushSecret"`
  25. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  26. WeChatPushUserId string `json:"weChatPushUserId"`
  27. TgBotToken string `json:"tgBotToken"`
  28. TgChatId string `json:"tgChatId"`
  29. IsInit bool `json:"isInit"`
  30. WebPushUrl string `json:"webPushUrl"`
  31. WebPushToken string `json:"webPushToken"`
  32. Tables map[string]string `json:"-"`
  33. TablesInitData map[string]string `json:"-"`
  34. setupPort int // 初始化阶段端口
  35. }
  36. func (c *Config) GetSetupPort() int {
  37. return c.setupPort
  38. }
  39. func (c *Config) SetSetupPort(setupPort int) {
  40. c.setupPort = setupPort
  41. }
  42. const DBTypeMySQL = "mysql"
  43. const DBTypeSQLite = "sqlite"
  44. const SSLTypeAutoHTTP = "0" //自动生成证书
  45. const SSLTypeAutoDNS = "2" //自动生成证书,DNS api验证
  46. const SSLTypeUser = "1" //用户上传证书
  47. var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
  48. var Instance *Config = &Config{}
  49. func Init() {
  50. var cfgData []byte
  51. var err error
  52. args := os.Args
  53. if len(args) >= 2 && args[len(args)-1] == "dev" {
  54. cfgData, err = os.ReadFile("./config/config.dev.json")
  55. if err != nil {
  56. return
  57. }
  58. } else {
  59. cfgData, err = os.ReadFile("./config/config.json")
  60. if err != nil {
  61. return
  62. }
  63. }
  64. err = json.Unmarshal(cfgData, &Instance)
  65. if err != nil {
  66. return
  67. }
  68. if len(Instance.Domains) == 0 && Instance.Domain != "" {
  69. Instance.Domains = []string{Instance.Domain}
  70. }
  71. if Instance.Domain != "" && Instance.IsInit {
  72. IsInit = true
  73. }
  74. }