config.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package config
  2. import (
  3. "flag"
  4. "time"
  5. "github.com/spf13/viper"
  6. )
  7. const defaultConfigPath = "./.env"
  8. type Service struct {
  9. appPort uint16
  10. corsAllowOrigins string
  11. corsAllowMethods string
  12. dbDriver string
  13. dbHost string
  14. dbPort uint16
  15. dbName string
  16. dbUser string
  17. dbPassword string
  18. dbMaxOpenConns uint16
  19. dbMaxIdleConns uint16
  20. dbMaxOpenConnLifetime time.Duration
  21. dbMaxIdleConnLifetime time.Duration
  22. cacheDefaultDuration time.Duration
  23. cacheCleanupInterval time.Duration
  24. smtpHost string
  25. smtpPort uint16
  26. smtpUser string
  27. smtpPassword string
  28. jwtSecretKey string
  29. jwtLifeTime time.Duration
  30. jwtCookie string
  31. basicAuthUser string
  32. basicAuthPassword string
  33. staticVersion uint16
  34. loggerStdoutEnabled bool
  35. loggerStdoutLevel string
  36. loggerEmailEnabled bool
  37. loggerEmailLevel string
  38. loggerEmailSubject string
  39. loggerEmailRecipient string
  40. loginRateLimiterMaxRequests uint16
  41. loginRateLimiterExpiration time.Duration
  42. }
  43. func Init() (*Service, error) {
  44. var configPath string
  45. flag.StringVar(&configPath, "config", "", "Path to .env config file")
  46. flag.Parse()
  47. if configPath == "" {
  48. configPath = defaultConfigPath
  49. }
  50. viper.SetConfigFile(configPath)
  51. viper.SetConfigType("env")
  52. viper.AutomaticEnv()
  53. if err := viper.ReadInConfig(); err != nil {
  54. return nil, err
  55. }
  56. s := struct {
  57. AppPort uint16 `mapstructure:"APP_PORT"`
  58. CorsAllowOriginsOrigins string `mapstructure:"CORS_ALLOW_ORIGIN"`
  59. CorsAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
  60. DBDriver string `mapstructure:"DB_DRIVER"`
  61. DBHost string `mapstructure:"DB_HOST"`
  62. DBPort uint16 `mapstructure:"DB_PORT"`
  63. DBName string `mapstructure:"DB_NAME"`
  64. DBUser string `mapstructure:"DB_USER"`
  65. DBPassword string `mapstructure:"DB_PASSWORD"`
  66. DBMaxOpenConns uint16 `mapstructure:"DB_MAX_OPEN_CONNS"`
  67. DBMaxIdleConns uint16 `mapstructure:"DB_MAX_IDLE_CONNS"`
  68. DBMaxOpenConnLifetime time.Duration `mapstructure:"DB_MAX_OPEN_CONN_LIFETIME"`
  69. DBMaxIdleConnLifetime time.Duration `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
  70. CacheDefaultDuration time.Duration `mapstructure:"CACHE_DEFAULT_EXPIRATION"`
  71. CacheCleanupInterval time.Duration `mapstructure:"CACHE_CLEANUP_INTERVAL"`
  72. SMTPHost string `mapstructure:"SMTP_HOST"`
  73. SMTPPort uint16 `mapstructure:"SMTP_PORT"`
  74. SMTPUser string `mapstructure:"SMTP_USER"`
  75. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  76. JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
  77. JWTLifeTime time.Duration `mapstructure:"JWT_LIFETIME"`
  78. JWTCookie string `mapstructure:"JWT_COOKIE"`
  79. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  80. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  81. StaticVersion uint16 `mapstructure:"STATIC_VERSION"`
  82. LoggerStdoutEnabled bool `mapstructure:"LOGGER_STDOUT_ENABLED"`
  83. LoggerStdoutLevel string `mapstructure:"LOGGER_STDOUT_LEVEL"`
  84. LoggerEmailEnabled bool `mapstructure:"LOGGER_EMAIL_ENABLED"`
  85. LoggerEmailLevel string `mapstructure:"LOGGER_EMAIL_LEVEL"`
  86. LoggerEmailSubject string `mapstructure:"LOGGER_EMAIL_SUBJECT"`
  87. LoggerEmailRecipient string `mapstructure:"LOGGER_EMAIL_RECIPIENT"`
  88. LoginRateLimiterMaxRequests uint16 `mapstructure:"LOGIN_RATE_LIMITER_MAX_REQUESTS"`
  89. LoginRateLimiterExpiration time.Duration `mapstructure:"LOGIN_RATE_LIMITER_EXPIRATION"`
  90. }{}
  91. if err := viper.Unmarshal(&s); err != nil {
  92. return nil, err
  93. }
  94. return &Service{
  95. appPort: s.AppPort,
  96. corsAllowOrigins: s.CorsAllowOriginsOrigins,
  97. corsAllowMethods: s.CorsAllowMethods,
  98. dbDriver: s.DBDriver,
  99. dbHost: s.DBHost,
  100. dbPort: s.DBPort,
  101. dbName: s.DBName,
  102. dbUser: s.DBUser,
  103. dbPassword: s.DBPassword,
  104. dbMaxOpenConns: s.DBMaxOpenConns,
  105. dbMaxIdleConns: s.DBMaxIdleConns,
  106. dbMaxOpenConnLifetime: s.DBMaxOpenConnLifetime,
  107. dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
  108. cacheDefaultDuration: s.CacheDefaultDuration,
  109. cacheCleanupInterval: s.CacheCleanupInterval,
  110. smtpHost: s.SMTPHost,
  111. smtpPort: s.SMTPPort,
  112. smtpUser: s.SMTPUser,
  113. smtpPassword: s.SMTPPassword,
  114. jwtSecretKey: s.JWTSecretKey,
  115. jwtLifeTime: s.JWTLifeTime,
  116. jwtCookie: s.JWTCookie,
  117. basicAuthUser: s.BasicAuthUser,
  118. basicAuthPassword: s.BasicAuthPassword,
  119. staticVersion: s.StaticVersion,
  120. loggerStdoutEnabled: s.LoggerStdoutEnabled,
  121. loggerStdoutLevel: s.LoggerStdoutLevel,
  122. loggerEmailEnabled: s.LoggerEmailEnabled,
  123. loggerEmailLevel: s.LoggerEmailLevel,
  124. loggerEmailSubject: s.LoggerEmailSubject,
  125. loggerEmailRecipient: s.LoggerEmailRecipient,
  126. loginRateLimiterMaxRequests: s.LoginRateLimiterMaxRequests,
  127. loginRateLimiterExpiration: s.LoginRateLimiterExpiration,
  128. }, nil
  129. }
  130. func (s Service) AppPort() uint16 {
  131. return s.appPort
  132. }
  133. func (s Service) CORSAllowOrigins() string {
  134. return s.corsAllowOrigins
  135. }
  136. func (s Service) CORSAllowMethods() string {
  137. return s.corsAllowMethods
  138. }
  139. func (s Service) DBDriver() string {
  140. return s.dbDriver
  141. }
  142. func (s Service) DBHost() string {
  143. return s.dbHost
  144. }
  145. func (s Service) DBPort() uint16 {
  146. return s.dbPort
  147. }
  148. func (s Service) DBName() string {
  149. return s.dbName
  150. }
  151. func (s Service) DBUser() string {
  152. return s.dbUser
  153. }
  154. func (s Service) DBPassword() string {
  155. return s.dbPassword
  156. }
  157. func (s Service) DBMaxOpenConns() uint16 {
  158. return s.dbMaxOpenConns
  159. }
  160. func (s Service) DBMaxIdleConns() uint16 {
  161. return s.dbMaxIdleConns
  162. }
  163. func (s Service) DBMaxOpenConnLifetime() time.Duration {
  164. return s.dbMaxOpenConnLifetime
  165. }
  166. func (s Service) DBMaxIdleConnLifetime() time.Duration {
  167. return s.dbMaxIdleConnLifetime
  168. }
  169. func (s Service) CacheDefaultDuration() time.Duration {
  170. return s.cacheDefaultDuration
  171. }
  172. func (s Service) CacheCleanupInterval() time.Duration {
  173. return s.cacheCleanupInterval
  174. }
  175. func (s Service) SMTPHost() string {
  176. return s.smtpHost
  177. }
  178. func (s Service) SMTPPort() uint16 {
  179. return s.smtpPort
  180. }
  181. func (s Service) SMTPUser() string {
  182. return s.smtpUser
  183. }
  184. func (s Service) SMTPPassword() string {
  185. return s.smtpPassword
  186. }
  187. func (s Service) JWTSecretKey() string {
  188. return s.jwtSecretKey
  189. }
  190. func (s Service) JWTLifeTime() time.Duration {
  191. return s.jwtLifeTime
  192. }
  193. func (s Service) JWTCookie() string {
  194. return s.jwtCookie
  195. }
  196. func (s Service) BasicAuthUser() string {
  197. return s.basicAuthUser
  198. }
  199. func (s Service) BasicAuthPassword() string {
  200. return s.basicAuthPassword
  201. }
  202. func (s Service) StaticVersion() uint16 {
  203. return s.staticVersion
  204. }
  205. func (s Service) LoggerStdoutEnabled() bool {
  206. return s.loggerStdoutEnabled
  207. }
  208. func (s Service) LoggerStdoutLevel() string {
  209. return s.loggerStdoutLevel
  210. }
  211. func (s Service) LoggerEmailEnabled() bool {
  212. return s.loggerEmailEnabled
  213. }
  214. func (s Service) LoggerEmailLevel() string {
  215. return s.loggerEmailLevel
  216. }
  217. func (s Service) LoggerEmailSubject() string {
  218. return s.loggerEmailSubject
  219. }
  220. func (s Service) LoggerEmailRecipient() string {
  221. return s.loggerEmailRecipient
  222. }
  223. func (s Service) LoginRateLimiterMaxRequests() uint16 {
  224. return s.loginRateLimiterMaxRequests
  225. }
  226. func (s Service) LoginRateLimiterExpiration() time.Duration {
  227. return s.loginRateLimiterExpiration
  228. }