config.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Domain string `json:"domain"`
  12. WebDomain string `json:"webDomain"`
  13. DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
  14. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  15. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  16. DbDSN string `json:"dbDSN"`
  17. DbType string `json:"dbType"`
  18. WeChatPushAppId string `json:"weChatPushAppId"`
  19. WeChatPushSecret string `json:"weChatPushSecret"`
  20. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  21. WeChatPushUserId string `json:"weChatPushUserId"`
  22. IsInit bool `json:"isInit"`
  23. Tables map[string]string `json:"-"`
  24. TablesInitData map[string]string `json:"-"`
  25. }
  26. //go:embed tables/*
  27. var tableConfig embed.FS
  28. const Version = "1.1.0"
  29. const DBTypeMySQL = "mysql"
  30. const DBTypeSQLite = "sqlite"
  31. var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
  32. var Instance *Config
  33. func Init() {
  34. var cfgData []byte
  35. var err error
  36. args := os.Args
  37. if len(args) >= 2 && args[len(args)-1] == "dev" {
  38. cfgData, err = os.ReadFile("./config/config.dev.json")
  39. if err != nil {
  40. return
  41. }
  42. } else {
  43. cfgData, err = os.ReadFile("./config/config.json")
  44. if err != nil {
  45. return
  46. }
  47. }
  48. err = json.Unmarshal(cfgData, &Instance)
  49. if err != nil {
  50. return
  51. }
  52. // 读取表设置
  53. Instance.Tables = map[string]string{}
  54. Instance.TablesInitData = map[string]string{}
  55. root := "tables/mysql"
  56. if Instance.DbType == DBTypeSQLite {
  57. root = "tables/sqlite"
  58. }
  59. err = fs.WalkDir(tableConfig, root, func(path string, info fs.DirEntry, err error) error {
  60. if !info.IsDir() && strings.HasSuffix(info.Name(), ".sql") {
  61. tableName := strings.ReplaceAll(info.Name(), ".sql", "")
  62. i, e := tableConfig.ReadFile(path)
  63. if e != nil {
  64. panic(e)
  65. }
  66. if strings.Contains(path, "data") {
  67. Instance.TablesInitData[tableName] = string(i)
  68. } else {
  69. Instance.Tables[tableName] = string(i)
  70. }
  71. }
  72. return nil
  73. })
  74. if err != nil {
  75. panic(err)
  76. }
  77. if Instance.Domain != "" && Instance.IsInit {
  78. IsInit = true
  79. }
  80. }