config.go 5.3 KB

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