config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package application
  2. import (
  3. "encoding/json"
  4. "os"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  6. "git.sxidc.com/go-tools/utils/fileutils"
  7. "github.com/pkg/errors"
  8. "gopkg.in/yaml.v3"
  9. )
  10. type Config struct {
  11. ApiConfig `json:"api" yaml:"api"`
  12. InfrastructureConfig `json:"infrastructure" yaml:"infrastructure"`
  13. *MqttApiConfig `json:"mqtt_api" yaml:"mqtt_api"`
  14. }
  15. type ApiConfig struct {
  16. UrlPrefix string `json:"url_prefix" yaml:"url_prefix"`
  17. Port string `json:"port" yaml:"port"`
  18. LogSkipPaths []string `json:"log_skip_paths" yaml:"log_skip_paths"`
  19. DumpPermissionItem *struct {
  20. Namespace string `json:"namespace" yaml:"namespace"`
  21. Gateway string `json:"gateway" yaml:"gateway"`
  22. SaveDir string `json:"save_dir" yaml:"save_dir"`
  23. } `json:"dump_permission_item" yaml:"dump_permission_item"`
  24. }
  25. type InfrastructureConfig struct {
  26. Database infrastructure.DatabaseConfig `json:"database" yaml:"database"`
  27. Cache infrastructure.CacheConfig `json:"cache" yaml:"cache"`
  28. MessageQueue infrastructure.MessageQueueConfig `json:"message_queue" yaml:"message_queue"`
  29. }
  30. type MqttApiConfig struct {
  31. TopicPrefix string `json:"topic_prefix" yaml:"topic_prefix"`
  32. LogSkipPaths []string `json:"log_skip_paths" yaml:"log_skip_paths"`
  33. MqttConfig MqttConfig `json:"mqtt_config" yaml:"mqtt_config"`
  34. }
  35. type MqttConfig struct {
  36. UserName string `json:"username" yaml:"username"`
  37. Password string `json:"password" yaml:"password"`
  38. Address string `json:"address" yaml:"address"`
  39. ClientID string `json:"client_id" yaml:"client_id"`
  40. KeepAliveSec int64 `json:"keep_alive_sec" yaml:"keep_alive_sec"`
  41. PingTimeoutSec int64 `json:"ping_timeout_sec" yaml:"ping_timeout_sec"`
  42. WriteTimeoutSec int64 `json:"write_timeout_sec" yaml:"write_timeout_sec"`
  43. }
  44. func LoadFromJsonFile(jsonFilePath string) (Config, error) {
  45. if !fileutils.PathExists(jsonFilePath) {
  46. return Config{}, errors.New("配置文件不存在")
  47. }
  48. jsonBytes, err := os.ReadFile(jsonFilePath)
  49. if err != nil {
  50. return Config{}, err
  51. }
  52. return loadFromJson(jsonBytes)
  53. }
  54. func LoadFromYamlFile(yamlFilePath string) (Config, error) {
  55. if !fileutils.PathExists(yamlFilePath) {
  56. return Config{}, errors.New("配置文件不存在")
  57. }
  58. yamlBytes, err := os.ReadFile(yamlFilePath)
  59. if err != nil {
  60. return Config{}, err
  61. }
  62. return loadFromYaml(yamlBytes)
  63. }
  64. func LoadFromJson(jsonStr string) (Config, error) {
  65. return loadFromJson([]byte(jsonStr))
  66. }
  67. func LoadFromYaml(yamlStr string) (Config, error) {
  68. return loadFromYaml([]byte(yamlStr))
  69. }
  70. func loadFromJson(jsonBytes []byte) (Config, error) {
  71. conf := new(Config)
  72. err := json.Unmarshal(jsonBytes, conf)
  73. if err != nil {
  74. return Config{}, errors.New(err.Error())
  75. }
  76. return *conf, nil
  77. }
  78. func loadFromYaml(yamlBytes []byte) (Config, error) {
  79. conf := new(Config)
  80. err := yaml.Unmarshal(yamlBytes, conf)
  81. if err != nil {
  82. return Config{}, errors.New(err.Error())
  83. }
  84. return *conf, nil
  85. }