config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "gopkg.in/yaml.v3"
  6. )
  7. // Config holds application configuration
  8. type Config struct {
  9. Server *Server `yaml:"server"`
  10. Database *Database `yaml:"database"`
  11. Auth *Auth `yaml:"auth"`
  12. Providers map[string]map[string]string `yaml:"providers"`
  13. Debug bool `yaml:"debug"`
  14. }
  15. func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
  16. type plain Config
  17. err := unmarshal((*plain)(c))
  18. if err != nil {
  19. return err
  20. }
  21. if c.Server == nil {
  22. return fmt.Errorf("server configuration is required")
  23. }
  24. if c.Database == nil {
  25. return fmt.Errorf("database configuration is required")
  26. }
  27. if c.Auth == nil {
  28. return fmt.Errorf("auth configuration is required")
  29. }
  30. if c.Providers == nil {
  31. return fmt.Errorf("at least one provider configuration is required")
  32. }
  33. return nil
  34. }
  35. // Server holds server configuration
  36. type Server struct {
  37. Host string `yaml:"host"`
  38. Port int `yaml:"port"`
  39. Tls *TlsConfig `yaml:"tls"`
  40. }
  41. func (c *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
  42. type plain Server
  43. err := unmarshal((*plain)(c))
  44. if err != nil {
  45. return err
  46. }
  47. if c.Host == "" {
  48. return fmt.Errorf("host is required")
  49. }
  50. if c.Port == 0 {
  51. c.Port = 443
  52. }
  53. if c.Tls == nil {
  54. return fmt.Errorf("TLS configuration is required")
  55. }
  56. return nil
  57. }
  58. // TlsConfig holds TLS configuration
  59. type TlsConfig struct {
  60. Enabled bool `yaml:"enabled"`
  61. CertFile string `yaml:"cert_file"`
  62. KeyFile string `yaml:"key_file"`
  63. }
  64. func (c *TlsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  65. type plain TlsConfig
  66. err := unmarshal((*plain)(c))
  67. if err != nil {
  68. return err
  69. }
  70. if (c.CertFile == "" || c.KeyFile == "") && c.Enabled {
  71. return fmt.Errorf("TLS cert file is required")
  72. }
  73. return nil
  74. }
  75. // Database holds database configuration
  76. type Database struct {
  77. Type string `yaml:"type"`
  78. Sql *Sql `yaml:"sql"`
  79. }
  80. func (c *Database) UnmarshalYAML(unmarshal func(interface{}) error) error {
  81. type plain Database
  82. err := unmarshal((*plain)(c))
  83. if err != nil {
  84. return err
  85. }
  86. if c.Type == "" {
  87. return fmt.Errorf("database type is required")
  88. }
  89. if c.Type != "sql" && c.Type != "memory" {
  90. return fmt.Errorf("unsupported database type: %s", c.Type)
  91. }
  92. if c.Type == "sql" && c.Sql == nil {
  93. return fmt.Errorf("SQL database configuration is required")
  94. }
  95. return nil
  96. }
  97. // Sql holds SQL database configuration
  98. type Sql struct {
  99. Host string `yaml:"host"`
  100. Port int `yaml:"port"`
  101. Username string `yaml:"username"`
  102. Password string `yaml:"password"`
  103. Name string `yaml:"name"`
  104. }
  105. func (c *Sql) UnmarshalYAML(unmarshal func(interface{}) error) error {
  106. type plain Sql
  107. err := unmarshal((*plain)(c))
  108. if err != nil {
  109. return err
  110. }
  111. if c.Host == "" {
  112. return fmt.Errorf("host is required")
  113. }
  114. if c.Port == 0 {
  115. c.Port = 5432 // Default PostgreSQL port
  116. }
  117. if c.Username == "" {
  118. return fmt.Errorf("username is required")
  119. }
  120. if c.Password == "" {
  121. return fmt.Errorf("password is required")
  122. }
  123. if c.Name == "" {
  124. return fmt.Errorf("database name is required")
  125. }
  126. return nil
  127. }
  128. // Auth holds authentication configuration
  129. type Auth struct {
  130. PrivateKey string `yaml:"private_key"`
  131. TokenDuration int `yaml:"token_duration"`
  132. CleanupInterval int `yaml:"cleanup_interval"`
  133. }
  134. func (c *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error {
  135. type plain Auth
  136. err := unmarshal((*plain)(c))
  137. if err != nil {
  138. return err
  139. }
  140. if c.PrivateKey == "" {
  141. return fmt.Errorf("private key is required")
  142. }
  143. if c.TokenDuration == 0 {
  144. c.TokenDuration = 3600 // Default to 1 hour
  145. }
  146. if c.CleanupInterval == 0 {
  147. c.CleanupInterval = 3600 // Default to 1 hour
  148. }
  149. return nil
  150. }
  151. func Load(configPath string) (*Config, error) {
  152. cnf := &Config{}
  153. b, err := os.ReadFile(configPath)
  154. if err != nil {
  155. return nil, err
  156. }
  157. err = yaml.Unmarshal(b, cnf)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return cnf, nil
  162. }