config.go 2.7 KB

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