config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. Sqlite *Sqlite `yaml:"sqlite"`
  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 != "sqlite" && c.Type != "memory" {
  90. return fmt.Errorf("unsupported database type: %s", c.Type)
  91. }
  92. if c.Type == "sqlite" && c.Sqlite == nil {
  93. return fmt.Errorf("SQL database configuration is required")
  94. }
  95. return nil
  96. }
  97. // Sql holds SQL database configuration
  98. type Sqlite struct {
  99. File string `yaml:"file"`
  100. }
  101. func (c *Sqlite) UnmarshalYAML(unmarshal func(interface{}) error) error {
  102. type plain Sqlite
  103. err := unmarshal((*plain)(c))
  104. if err != nil {
  105. return err
  106. }
  107. if c.File == "" {
  108. return fmt.Errorf("SQLite file is required")
  109. }
  110. return nil
  111. }
  112. // Auth holds authentication configuration
  113. type Auth struct {
  114. PrivateKey string `yaml:"private_key"`
  115. TokenDuration int `yaml:"token_duration"`
  116. CleanupInterval int `yaml:"cleanup_interval"`
  117. }
  118. func (c *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error {
  119. type plain Auth
  120. err := unmarshal((*plain)(c))
  121. if err != nil {
  122. return err
  123. }
  124. if c.PrivateKey == "" {
  125. return fmt.Errorf("private key is required")
  126. }
  127. if c.TokenDuration == 0 {
  128. c.TokenDuration = 3600 // Default to 1 hour
  129. }
  130. if c.CleanupInterval == 0 {
  131. c.CleanupInterval = 3600 // Default to 1 hour
  132. }
  133. return nil
  134. }
  135. func Load(configPath string) (*Config, error) {
  136. cnf := &Config{}
  137. b, err := os.ReadFile(configPath)
  138. if err != nil {
  139. return nil, err
  140. }
  141. err = yaml.Unmarshal(b, cnf)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return cnf, nil
  146. }