config.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package config
  2. import (
  3. "embed"
  4. "encoding/json"
  5. "io/fs"
  6. "os"
  7. "strings"
  8. )
  9. type Config struct {
  10. Domain string `json:"domain"`
  11. DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
  12. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  13. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  14. MysqlDSN string `json:"mysqlDSN"`
  15. WeChatPushAppId string `json:"weChatPushAppId"`
  16. WeChatPushSecret string `json:"weChatPushSecret"`
  17. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  18. WeChatPushUserId string `json:"weChatPushUserId"`
  19. Tables map[string]string
  20. TablesInitData map[string]string
  21. }
  22. //go:embed tables/*
  23. var tableConfig embed.FS
  24. var Instance *Config
  25. func Init() {
  26. var cfgData []byte
  27. var err error
  28. args := os.Args
  29. if len(args) >= 2 && args[len(args)-1] == "dev" {
  30. cfgData, err = os.ReadFile("./config/config.dev.json")
  31. if err != nil {
  32. panic("dev环境配置文件加载失败" + err.Error())
  33. }
  34. } else {
  35. cfgData, err = os.ReadFile("./config/config.json")
  36. if err != nil {
  37. panic("配置文件加载失败" + err.Error())
  38. }
  39. }
  40. err = json.Unmarshal(cfgData, &Instance)
  41. if err != nil {
  42. panic("配置文件加载失败" + err.Error())
  43. }
  44. // 读取表设置
  45. Instance.Tables = map[string]string{}
  46. Instance.TablesInitData = map[string]string{}
  47. err = fs.WalkDir(tableConfig, "tables", func(path string, info fs.DirEntry, err error) error {
  48. if !info.IsDir() && strings.HasSuffix(info.Name(), ".sql") {
  49. tableName := strings.ReplaceAll(info.Name(), ".sql", "")
  50. i, e := tableConfig.ReadFile(path)
  51. if e != nil {
  52. panic(e)
  53. }
  54. if strings.Contains(path, "data") {
  55. Instance.TablesInitData[tableName] = string(i)
  56. } else {
  57. Instance.Tables[tableName] = string(i)
  58. }
  59. }
  60. return nil
  61. })
  62. if err != nil {
  63. panic(err)
  64. }
  65. }