config.go 8.9 KB

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