config.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // pkg/config/config.go
  2. package config
  3. import (
  4. "fmt"
  5. "os"
  6. "git.linuxforward.com/byom/byom-golang-lib/pkg/auth"
  7. "git.linuxforward.com/byom/byom-golang-lib/pkg/database"
  8. "git.linuxforward.com/byom/byom-golang-lib/pkg/logger"
  9. "git.linuxforward.com/byom/byom-golang-lib/pkg/server"
  10. "git.linuxforward.com/byom/byom-golang-lib/pkg/storage"
  11. "gopkg.in/yaml.v3"
  12. )
  13. type Config struct {
  14. App *App `yaml:"app"`
  15. Auth *auth.Config `yaml:"auth"`
  16. Server *server.Config `yaml:"server"`
  17. Database *database.Config `yaml:"database"`
  18. Log *logger.Config `yaml:"log"`
  19. Storage *storage.Config `yaml:"storage"`
  20. CloudComputing *CloudComputing `yaml:"cloud_computing,omitempty"`
  21. SocialNetworks *SocialNetworks `yaml:"social_networks,omitempty"`
  22. }
  23. func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
  24. type plain Config
  25. err := unmarshal((*plain)(c))
  26. if err != nil {
  27. return err
  28. }
  29. if c.App == nil {
  30. return NewFieldError("validate", "root", "app", "", ErrMissingRequired, "app section is required")
  31. }
  32. // Conditional validation based on app name
  33. switch c.App.Name {
  34. case "design":
  35. if c.CloudComputing == nil {
  36. return NewFieldError("validate", "root", "cloud_computing", "", ErrMissingRequired, "cloud_computing section is required for design app")
  37. }
  38. case "trends":
  39. if c.SocialNetworks == nil {
  40. return NewFieldError("validate", "root", "social_networks", "", ErrMissingRequired, "social_networks section is required for trends app")
  41. }
  42. }
  43. return nil
  44. }
  45. type App struct {
  46. Name string `yaml:"name"`
  47. Environment string `yaml:"environment"`
  48. }
  49. func (c *App) UnmarshalYAML(unmarshal func(interface{}) error) error {
  50. type plain App
  51. err := unmarshal((*plain)(c))
  52. if err != nil {
  53. return err
  54. }
  55. if c.Name == "" {
  56. return NewFieldError("validate", "app", "name", "", ErrMissingRequired, "app name is required")
  57. }
  58. if c.Environment == "" {
  59. c.Environment = "development"
  60. }
  61. // Validate app name
  62. validNames := map[string]bool{"core": true, "design": true, "trends": true}
  63. if !validNames[c.Name] {
  64. return NewFieldError("validate", "app", "name", c.Name, ErrInvalidAppName, "app name must be one of: core, design, trends")
  65. }
  66. return nil
  67. }
  68. type CloudComputing struct {
  69. Provider string `yaml:"provider"`
  70. Region string `yaml:"region"`
  71. MaxCapacity int `yaml:"max_capacity"`
  72. }
  73. func (c *CloudComputing) UnmarshalYAML(unmarshal func(interface{}) error) error {
  74. type plain CloudComputing
  75. err := unmarshal((*plain)(c))
  76. if err != nil {
  77. return err
  78. }
  79. if c.Provider == "" {
  80. return NewFieldError("validate", "cloud_computing", "provider", "", ErrMissingRequired, "provider is required")
  81. }
  82. if c.Region == "" {
  83. return NewFieldError("validate", "cloud_computing", "region", "", ErrMissingRequired, "region is required")
  84. }
  85. if c.MaxCapacity <= 0 {
  86. return NewFieldError("validate", "cloud_computing", "max_capacity", fmt.Sprintf("%d", c.MaxCapacity), ErrInvalidValue, "max_capacity must be greater than 0")
  87. }
  88. return nil
  89. }
  90. type SocialNetworks struct {
  91. Networks []SocialNetwork `yaml:"networks"`
  92. }
  93. type SocialNetwork struct {
  94. Name string `yaml:"name"`
  95. APIKey string `yaml:"api_key"`
  96. APIToken string `yaml:"api_token"`
  97. }
  98. func (c *SocialNetworks) UnmarshalYAML(unmarshal func(interface{}) error) error {
  99. type plain SocialNetworks
  100. err := unmarshal((*plain)(c))
  101. if err != nil {
  102. return err
  103. }
  104. if len(c.Networks) == 0 {
  105. return NewFieldError("validate", "social_networks", "networks", "", ErrMissingRequired, "at least one social network is required")
  106. }
  107. for i, network := range c.Networks {
  108. section := fmt.Sprintf("social_networks.networks[%d]", i)
  109. if network.Name == "" {
  110. return NewFieldError("validate", section, "name", "", ErrMissingRequired, "network name is required")
  111. }
  112. if network.APIKey == "" {
  113. return NewFieldError("validate", section, "api_key", "", ErrMissingRequired, "API key is required")
  114. }
  115. if network.APIToken == "" {
  116. return NewFieldError("validate", section, "api_token", "", ErrMissingRequired, "API token is required")
  117. }
  118. }
  119. return nil
  120. }
  121. func ReadConfig(configPath string) (*Config, error) {
  122. data, err := os.ReadFile(configPath)
  123. if err != nil {
  124. if os.IsNotExist(err) {
  125. return nil, NewError("read", ErrFileNotFound, configPath)
  126. }
  127. if os.IsPermission(err) {
  128. return nil, NewError("read", ErrFilePermission, configPath)
  129. }
  130. return nil, NewError("read", err, "failed to read config file")
  131. }
  132. config := &Config{}
  133. err = yaml.Unmarshal(data, config)
  134. if err != nil {
  135. return nil, NewError("parse", ErrInvalidYAML, err.Error())
  136. }
  137. // Validate all config sections that implement Validator interface
  138. validators := []Validator{}
  139. if config.Storage != nil {
  140. validators = append(validators, config.Storage)
  141. }
  142. if config.Database != nil {
  143. validators = append(validators, config.Database)
  144. }
  145. if config.Log != nil {
  146. validators = append(validators, config.Log)
  147. }
  148. if config.Server != nil {
  149. validators = append(validators, config.Server)
  150. }
  151. if config.Auth != nil {
  152. validators = append(validators, config.Auth)
  153. }
  154. if err := ValidateAll(validators...); err != nil {
  155. return nil, err
  156. }
  157. return config, nil
  158. }