config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. log "github.com/sirupsen/logrus"
  6. "gopkg.in/yaml.v3"
  7. )
  8. type Config struct {
  9. Server *Server `yaml:"server"`
  10. Database *Database `yaml:"database"`
  11. Log *Log `yaml:"log"`
  12. Jwt *Jwt `yaml:"jwt"`
  13. Smtp *Smtp `yaml:"smtp"`
  14. Oauth2 *Oauth2 `yaml:"oauth2"`
  15. Hook *Hook `yaml:"hook"`
  16. }
  17. func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
  18. type plain Config
  19. err := unmarshal((*plain)(c))
  20. if err != nil {
  21. return err
  22. }
  23. if c.Server == nil {
  24. return fmt.Errorf("api config is required")
  25. }
  26. return nil
  27. }
  28. type Server struct {
  29. ListeningPort int `yaml:"listening_port"`
  30. TlsConfig *TlsConfig `yaml:"tls"`
  31. Opts []string `yaml:"opts"`
  32. }
  33. func (c *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
  34. type plain Server
  35. err := unmarshal((*plain)(c))
  36. if err != nil {
  37. return err
  38. }
  39. if c.ListeningPort == 0 {
  40. c.ListeningPort = 8443
  41. }
  42. if c.TlsConfig == nil {
  43. return fmt.Errorf("tls is required")
  44. }
  45. return nil
  46. }
  47. type TlsConfig struct {
  48. Enabled bool `yaml:"enabled"`
  49. CertFile string `yaml:"cert"`
  50. KeyFile string `yaml:"key"`
  51. }
  52. func (c *TlsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
  53. type plain TlsConfig
  54. err := unmarshal((*plain)(c))
  55. if err != nil {
  56. return err
  57. }
  58. if c.CertFile == "" && c.Enabled {
  59. return fmt.Errorf("tls cert is required")
  60. }
  61. if c.KeyFile == "" && c.Enabled {
  62. return fmt.Errorf("tls key is required")
  63. }
  64. return nil
  65. }
  66. type Smtp struct {
  67. Host string `yaml:"host"`
  68. Port int `yaml:"port"`
  69. User string `yaml:"user"`
  70. Pass string `yaml:"pass"`
  71. }
  72. func (c *Smtp) UnmarshalYAML(unmarshal func(interface{}) error) error {
  73. type plain Smtp
  74. err := unmarshal((*plain)(c))
  75. if err != nil {
  76. return err
  77. }
  78. // if c.Host == "" {
  79. // return fmt.Errorf("smtp host is required")
  80. // }
  81. // if c.Port == 0 {
  82. // c.Port = 587
  83. // }
  84. // if c.User == "" {
  85. // return fmt.Errorf("smtp user is required")
  86. // }
  87. // if c.Pass == "" {
  88. // return fmt.Errorf("smtp pass is required")
  89. // }
  90. return nil
  91. }
  92. type Database struct {
  93. Path string `yaml:"path"`
  94. LogMode bool `yaml:"log_mode"`
  95. MaxOpenConns int `yaml:"max_open_conns"`
  96. MaxIdleConns int `yaml:"max_idle_conns"`
  97. ConnMaxLifetime int `yaml:"conn_max_lifetime"`
  98. Pragma []string `yaml:"pragma"`
  99. }
  100. func (c *Database) UnmarshalYAML(unmarshal func(interface{}) error) error {
  101. type plain Database
  102. err := unmarshal((*plain)(c))
  103. if err != nil {
  104. return err
  105. }
  106. if c.Path == "" {
  107. return fmt.Errorf("database path is required")
  108. }
  109. if c.MaxOpenConns == 0 {
  110. c.MaxOpenConns = 10
  111. }
  112. if c.MaxIdleConns == 0 {
  113. c.MaxIdleConns = 5
  114. }
  115. if c.ConnMaxLifetime == 0 {
  116. c.ConnMaxLifetime = 300
  117. }
  118. return nil
  119. }
  120. type Jwt struct {
  121. JwtSecret string `yaml:"jwt_secret"`
  122. }
  123. func (c *Jwt) UnmarshalYAML(unmarshal func(interface{}) error) error {
  124. type plain Jwt
  125. err := unmarshal((*plain)(c))
  126. if err != nil {
  127. return err
  128. }
  129. if c.JwtSecret == "" {
  130. return fmt.Errorf("jwt secret is required")
  131. }
  132. return nil
  133. }
  134. type Oauth2 struct {
  135. ClientID string `yaml:"client_id"`
  136. ClientSecret string `yaml:"client_secret"`
  137. RedirectURL string `yaml:"redirect_url"`
  138. AuthURL string `yaml:"auth_url"`
  139. TokenURL string `yaml:"token_url"`
  140. UserInfoURL string `yaml:"user_info_url"`
  141. }
  142. func (c *Oauth2) UnmarshalYAML(unmarshal func(interface{}) error) error {
  143. type plain Oauth2
  144. err := unmarshal((*plain)(c))
  145. if err != nil {
  146. return err
  147. }
  148. if c.ClientID == "" {
  149. return fmt.Errorf("client id is required")
  150. }
  151. if c.ClientSecret == "" {
  152. return fmt.Errorf("client secret is required")
  153. }
  154. if c.RedirectURL == "" {
  155. return fmt.Errorf("redirect url is required")
  156. }
  157. if c.AuthURL == "" {
  158. return fmt.Errorf("auth url is required")
  159. }
  160. if c.TokenURL == "" {
  161. return fmt.Errorf("token url is required")
  162. }
  163. if c.UserInfoURL == "" {
  164. return fmt.Errorf("user info url is required")
  165. }
  166. return nil
  167. }
  168. type Log struct {
  169. Level string `yaml:"level"`
  170. NoColor bool `yaml:"no_color"`
  171. ForceColors bool `yaml:"force_colors"`
  172. InJson bool `yaml:"in_json"`
  173. FilterComponents []string `yaml:"filter_components"`
  174. }
  175. func (c *Log) UnmarshalYAML(unmarshal func(interface{}) error) error {
  176. type plain Log
  177. err := unmarshal((*plain)(c))
  178. if err != nil {
  179. return err
  180. }
  181. log.SetFormatter(&log.TextFormatter{
  182. ForceColors: c.ForceColors,
  183. DisableColors: c.NoColor,
  184. FullTimestamp: true,
  185. })
  186. if c.Level != "" {
  187. lvl, err := log.ParseLevel(c.Level)
  188. if err != nil {
  189. return err
  190. }
  191. log.SetLevel(lvl)
  192. }
  193. if c.InJson {
  194. log.SetFormatter(&log.JSONFormatter{})
  195. }
  196. return nil
  197. }
  198. type Hook struct {
  199. BaseURL string `yaml:"base_url"`
  200. Domain string `yaml:"domain"`
  201. SecretKey string `yaml:"secret_key"`
  202. }
  203. func ReadConfig(configPath string) (*Config, error) {
  204. cnf := &Config{}
  205. b, err := os.ReadFile(configPath)
  206. if err != nil {
  207. return nil, err
  208. }
  209. err = yaml.Unmarshal(b, cnf)
  210. if err != nil {
  211. return nil, err
  212. }
  213. return cnf, nil
  214. }