config.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // pkg/config/config.go
  2. package config
  3. import (
  4. "fmt"
  5. "os"
  6. "git.linuxforward.com/byom/byom-golang-lib/pkg/errors"
  7. "github.com/sirupsen/logrus"
  8. "gopkg.in/yaml.v3"
  9. )
  10. type Config struct {
  11. App *App `yaml:"app"`
  12. Server *Server `yaml:"server"`
  13. Database *Database `yaml:"database"`
  14. Log *Log `yaml:"log"`
  15. Storage *Storage `yaml:"storage"`
  16. CloudComputing *CloudComputing `yaml:"cloud_computing,omitempty"`
  17. SocialNetworks *SocialNetworks `yaml:"social_networks,omitempty"`
  18. }
  19. func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
  20. type plain Config
  21. err := unmarshal((*plain)(c))
  22. if err != nil {
  23. return err
  24. }
  25. if c.App == nil {
  26. return errors.NewConfigError("app", errors.ErrInvalidInput)
  27. }
  28. // Conditional validation based on app name
  29. switch c.App.Name {
  30. case "design":
  31. if c.CloudComputing == nil {
  32. return errors.NewConfigError("cloud_computing", errors.ErrInvalidInput)
  33. }
  34. case "trends":
  35. if c.SocialNetworks == nil {
  36. return errors.NewConfigError("social_networks", errors.ErrInvalidInput)
  37. }
  38. }
  39. return nil
  40. }
  41. type App struct {
  42. Name string `yaml:"name"`
  43. Environment string `yaml:"environment"`
  44. }
  45. func (c *App) UnmarshalYAML(unmarshal func(interface{}) error) error {
  46. type plain App
  47. err := unmarshal((*plain)(c))
  48. if err != nil {
  49. return err
  50. }
  51. if c.Name == "" {
  52. return errors.NewConfigError("app.name", errors.ErrInvalidInput)
  53. }
  54. if c.Environment == "" {
  55. c.Environment = "development"
  56. }
  57. // Validate app name
  58. validNames := map[string]bool{"core": true, "design": true, "trends": true}
  59. if !validNames[c.Name] {
  60. return errors.NewConfigError("app.name", errors.ErrInvalidInput)
  61. }
  62. return nil
  63. }
  64. type Server struct {
  65. Port int `yaml:"port"`
  66. RequestTimeout int `yaml:"request_timeout"` // in seconds
  67. AllowedOrigins []string `yaml:"allowed_origins"`
  68. }
  69. func (c *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
  70. type plain Server
  71. err := unmarshal((*plain)(c))
  72. if err != nil {
  73. return err
  74. }
  75. if c.Port == 0 {
  76. c.Port = 8080
  77. }
  78. if c.RequestTimeout == 0 {
  79. c.RequestTimeout = 30
  80. }
  81. return nil
  82. }
  83. type Database struct {
  84. Path string `yaml:"path"`
  85. MaxOpenConns int `yaml:"max_open_conns"`
  86. MaxIdleConns int `yaml:"max_idle_conns"`
  87. ConnMaxLifetime int `yaml:"conn_max_lifetime"`
  88. Pragmas []string `yaml:"pragmas"`
  89. }
  90. func (c *Database) UnmarshalYAML(unmarshal func(interface{}) error) error {
  91. type plain Database
  92. err := unmarshal((*plain)(c))
  93. if err != nil {
  94. return err
  95. }
  96. if c.Path == "" {
  97. return errors.NewConfigError("database.path", errors.ErrInvalidInput)
  98. }
  99. if c.MaxOpenConns == 0 {
  100. c.MaxOpenConns = 1 // SQLite recommendation
  101. }
  102. if c.MaxIdleConns == 0 {
  103. c.MaxIdleConns = 1
  104. }
  105. if c.ConnMaxLifetime == 0 {
  106. c.ConnMaxLifetime = 300 // 5 minutes
  107. }
  108. return nil
  109. }
  110. type Log struct {
  111. Level string `yaml:"level"`
  112. Format string `yaml:"format"` // "json" or "text"
  113. NoColor bool `yaml:"no_color"`
  114. }
  115. func (c *Log) UnmarshalYAML(unmarshal func(interface{}) error) error {
  116. type plain Log
  117. err := unmarshal((*plain)(c))
  118. if err != nil {
  119. return err
  120. }
  121. if c.Level == "" {
  122. c.Level = "info"
  123. }
  124. if c.Format == "" {
  125. c.Format = "text"
  126. }
  127. // Validate log level
  128. _, err = logrus.ParseLevel(c.Level)
  129. if err != nil {
  130. return errors.NewConfigError("log.level", err)
  131. }
  132. return nil
  133. }
  134. type Storage struct {
  135. Endpoint string `yaml:"endpoint"`
  136. AccessKeyID string `yaml:"access_key_id"`
  137. SecretAccessKey string `yaml:"secret_access_key"`
  138. UseSSL bool `yaml:"use_ssl"`
  139. BucketName string `yaml:"bucket_name"`
  140. }
  141. func (c *Storage) UnmarshalYAML(unmarshal func(interface{}) error) error {
  142. type plain Storage
  143. err := unmarshal((*plain)(c))
  144. if err != nil {
  145. return err
  146. }
  147. if c.Endpoint == "" {
  148. return errors.NewConfigError("storage.endpoint", errors.ErrInvalidInput)
  149. }
  150. if c.AccessKeyID == "" {
  151. return errors.NewConfigError("storage.access_key_id", errors.ErrInvalidInput)
  152. }
  153. if c.SecretAccessKey == "" {
  154. return errors.NewConfigError("storage.secret_access_key", errors.ErrInvalidInput)
  155. }
  156. if c.BucketName == "" {
  157. return errors.NewConfigError("storage.bucket_name", errors.ErrInvalidInput)
  158. }
  159. return nil
  160. }
  161. type CloudComputing struct {
  162. Provider string `yaml:"provider"`
  163. Region string `yaml:"region"`
  164. MaxCapacity int `yaml:"max_capacity"`
  165. }
  166. func (c *CloudComputing) UnmarshalYAML(unmarshal func(interface{}) error) error {
  167. type plain CloudComputing
  168. err := unmarshal((*plain)(c))
  169. if err != nil {
  170. return err
  171. }
  172. if c.Provider == "" {
  173. return errors.NewConfigError("cloud_computing.provider", errors.ErrInvalidInput)
  174. }
  175. if c.Region == "" {
  176. return errors.NewConfigError("cloud_computing.region", errors.ErrInvalidInput)
  177. }
  178. if c.MaxCapacity <= 0 {
  179. return errors.NewConfigError("cloud_computing.max_capacity", errors.ErrInvalidInput)
  180. }
  181. return nil
  182. }
  183. type SocialNetworks struct {
  184. Networks []SocialNetwork `yaml:"networks"`
  185. }
  186. type SocialNetwork struct {
  187. Name string `yaml:"name"`
  188. APIKey string `yaml:"api_key"`
  189. APIToken string `yaml:"api_token"`
  190. }
  191. func (c *SocialNetworks) UnmarshalYAML(unmarshal func(interface{}) error) error {
  192. type plain SocialNetworks
  193. err := unmarshal((*plain)(c))
  194. if err != nil {
  195. return err
  196. }
  197. if len(c.Networks) == 0 {
  198. return errors.NewConfigError("social_networks", errors.ErrInvalidInput)
  199. }
  200. for i, network := range c.Networks {
  201. section := fmt.Sprintf("social_networks.networks[%d]", i)
  202. if network.Name == "" {
  203. return errors.NewConfigError(section+".name", errors.ErrInvalidInput)
  204. }
  205. if network.APIKey == "" {
  206. return errors.NewConfigError(section+".api_key", errors.ErrInvalidInput)
  207. }
  208. if network.APIToken == "" {
  209. return errors.NewConfigError(section+".api_token", errors.ErrInvalidInput)
  210. }
  211. }
  212. return nil
  213. }
  214. func ReadConfig(configPath string) (*Config, error) {
  215. data, err := os.ReadFile(configPath)
  216. if err != nil {
  217. return nil, errors.NewConfigError("read", err)
  218. }
  219. config := &Config{}
  220. err = yaml.Unmarshal(data, config)
  221. if err != nil {
  222. return nil, errors.NewConfigError("parse", err)
  223. }
  224. return config, nil
  225. }