config.go 8.7 KB

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