config.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package config
  2. import (
  3. "time"
  4. "github.com/joho/godotenv"
  5. "github.com/kelseyhightower/envconfig"
  6. customErrors "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
  7. )
  8. type Service struct {
  9. appPort uint16
  10. appStaticPath string
  11. appTemplatesPath string
  12. corsAllowOrigins string
  13. corsAllowMethods string
  14. dbDriver string
  15. dbHost string
  16. dbPort uint16
  17. dbName string
  18. dbUser string
  19. dbPassword string
  20. dbMaxOpenConns uint16
  21. dbMaxIdleConns uint16
  22. dbMaxOpenConnLifetime time.Duration
  23. dbMaxIdleConnLifetime time.Duration
  24. cacheDefaultDuration 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. loggerFileEnabled bool
  38. loggerFileLevel string
  39. loggerFilePath string
  40. loggerEmailEnabled bool
  41. loggerEmailLevel string
  42. loggerEmailSubject string
  43. loggerEmailRecipient string
  44. loginRateLimiterMaxRequests uint16
  45. loginRateLimiterExpiration time.Duration
  46. }
  47. func Init() (*Service, error) {
  48. s := struct {
  49. AppPort uint16 `envconfig:"APP_PORT"`
  50. AppStaticPath string `envconfig:"APP_STATIC_PATH"`
  51. AppTemplatesPath string `envconfig:"APP_TEMPLATES_PATH"`
  52. CorsAllowOriginsOrigins string `envconfig:"CORS_ALLOW_ORIGIN"`
  53. CorsAllowMethods string `envconfig:"CORS_ALLOW_METHODS"`
  54. DBDriver string `envconfig:"DB_DRIVER"`
  55. DBHost string `envconfig:"DB_HOST"`
  56. DBPort uint16 `envconfig:"DB_PORT"`
  57. DBName string `envconfig:"DB_NAME"`
  58. DBUser string `envconfig:"DB_USER"`
  59. DBPassword string `envconfig:"DB_PASSWORD"`
  60. DBMaxOpenConns uint16 `envconfig:"DB_MAX_OPEN_CONNS"`
  61. DBMaxIdleConns uint16 `envconfig:"DB_MAX_IDLE_CONNS"`
  62. DBMaxOpenConnLifetime time.Duration `envconfig:"DB_MAX_OPEN_CONN_LIFETIME"`
  63. DBMaxIdleConnLifetime time.Duration `envconfig:"DB_MAX_IDLE_CONN_LIFETIME"`
  64. CacheDefaultDuration time.Duration `envconfig:"CACHE_DEFAULT_EXPIRATION"`
  65. SMTPHost string `envconfig:"SMTP_HOST"`
  66. SMTPPort uint16 `envconfig:"SMTP_PORT"`
  67. SMTPUser string `envconfig:"SMTP_USER"`
  68. SMTPPassword string `envconfig:"SMTP_PASSWORD"`
  69. JWTSecretKey string `envconfig:"JWT_SECRET_KEY"`
  70. JWTLifeTime time.Duration `envconfig:"JWT_LIFETIME"`
  71. JWTCookie string `envconfig:"JWT_COOKIE"`
  72. BasicAuthUser string `envconfig:"BASIC_AUTH_USER"`
  73. BasicAuthPassword string `envconfig:"BASIC_AUTH_PASSWORD"`
  74. StaticVersion uint16 `envconfig:"STATIC_VERSION"`
  75. LoggerStdoutEnabled bool `envconfig:"LOGGER_STDOUT_ENABLED"`
  76. LoggerStdoutLevel string `envconfig:"LOGGER_STDOUT_LEVEL"`
  77. LoggerFileEnabled bool `envconfig:"LOGGER_FILE_ENABLED"`
  78. LoggerFileLevel string `envconfig:"LOGGER_FILE_LEVEL"`
  79. LoggerFilePath string `envconfig:"LOGGER_FILE_PATH"`
  80. LoggerEmailEnabled bool `envconfig:"LOGGER_EMAIL_ENABLED"`
  81. LoggerEmailLevel string `envconfig:"LOGGER_EMAIL_LEVEL"`
  82. LoggerEmailSubject string `envconfig:"LOGGER_EMAIL_SUBJECT"`
  83. LoggerEmailRecipient string `envconfig:"LOGGER_EMAIL_RECIPIENT"`
  84. LoginRateLimiterMaxRequests uint16 `envconfig:"LOGIN_RATE_LIMITER_MAX_REQUESTS"`
  85. LoginRateLimiterExpiration time.Duration `envconfig:"LOGIN_RATE_LIMITER_EXPIRATION"`
  86. }{}
  87. _ = godotenv.Overload()
  88. if err := envconfig.Process("", &s); err != nil {
  89. return nil, customErrors.Wrap(err, "process config")
  90. }
  91. return &Service{
  92. appPort: s.AppPort,
  93. appStaticPath: s.AppStaticPath,
  94. appTemplatesPath: s.AppTemplatesPath,
  95. corsAllowOrigins: s.CorsAllowOriginsOrigins,
  96. corsAllowMethods: s.CorsAllowMethods,
  97. dbDriver: s.DBDriver,
  98. dbHost: s.DBHost,
  99. dbPort: s.DBPort,
  100. dbName: s.DBName,
  101. dbUser: s.DBUser,
  102. dbPassword: s.DBPassword,
  103. dbMaxOpenConns: s.DBMaxOpenConns,
  104. dbMaxIdleConns: s.DBMaxIdleConns,
  105. dbMaxOpenConnLifetime: s.DBMaxOpenConnLifetime,
  106. dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
  107. cacheDefaultDuration: s.CacheDefaultDuration,
  108. smtpHost: s.SMTPHost,
  109. smtpPort: s.SMTPPort,
  110. smtpUser: s.SMTPUser,
  111. smtpPassword: s.SMTPPassword,
  112. jwtSecretKey: s.JWTSecretKey,
  113. jwtLifeTime: s.JWTLifeTime,
  114. jwtCookie: s.JWTCookie,
  115. basicAuthUser: s.BasicAuthUser,
  116. basicAuthPassword: s.BasicAuthPassword,
  117. staticVersion: s.StaticVersion,
  118. loggerStdoutEnabled: s.LoggerStdoutEnabled,
  119. loggerStdoutLevel: s.LoggerStdoutLevel,
  120. loggerFileEnabled: s.LoggerFileEnabled,
  121. loggerFileLevel: s.LoggerFileLevel,
  122. loggerFilePath: s.LoggerFilePath,
  123. loggerEmailEnabled: s.LoggerEmailEnabled,
  124. loggerEmailLevel: s.LoggerEmailLevel,
  125. loggerEmailSubject: s.LoggerEmailSubject,
  126. loggerEmailRecipient: s.LoggerEmailRecipient,
  127. loginRateLimiterMaxRequests: s.LoginRateLimiterMaxRequests,
  128. loginRateLimiterExpiration: s.LoginRateLimiterExpiration,
  129. }, nil
  130. }
  131. func (s Service) AppPort() uint16 {
  132. return s.appPort
  133. }
  134. func (s Service) AppStaticPath() string {
  135. return s.appStaticPath
  136. }
  137. func (s Service) AppTemplatesPath() string {
  138. return s.appTemplatesPath
  139. }
  140. func (s Service) CORSAllowOrigins() string {
  141. return s.corsAllowOrigins
  142. }
  143. func (s Service) CORSAllowMethods() string {
  144. return s.corsAllowMethods
  145. }
  146. func (s Service) DBDriver() string {
  147. return s.dbDriver
  148. }
  149. func (s Service) DBHost() string {
  150. return s.dbHost
  151. }
  152. func (s Service) DBPort() uint16 {
  153. return s.dbPort
  154. }
  155. func (s Service) DBName() string {
  156. return s.dbName
  157. }
  158. func (s Service) DBUser() string {
  159. return s.dbUser
  160. }
  161. func (s Service) DBPassword() string {
  162. return s.dbPassword
  163. }
  164. func (s Service) DBMaxOpenConns() uint16 {
  165. return s.dbMaxOpenConns
  166. }
  167. func (s Service) DBMaxIdleConns() uint16 {
  168. return s.dbMaxIdleConns
  169. }
  170. func (s Service) DBMaxOpenConnLifetime() time.Duration {
  171. return s.dbMaxOpenConnLifetime
  172. }
  173. func (s Service) DBMaxIdleConnLifetime() time.Duration {
  174. return s.dbMaxIdleConnLifetime
  175. }
  176. func (s Service) CacheDefaultDuration() time.Duration {
  177. return s.cacheDefaultDuration
  178. }
  179. func (s Service) SMTPHost() string {
  180. return s.smtpHost
  181. }
  182. func (s Service) SMTPPort() uint16 {
  183. return s.smtpPort
  184. }
  185. func (s Service) SMTPUser() string {
  186. return s.smtpUser
  187. }
  188. func (s Service) SMTPPassword() string {
  189. return s.smtpPassword
  190. }
  191. func (s Service) JWTSecretKey() string {
  192. return s.jwtSecretKey
  193. }
  194. func (s Service) JWTLifeTime() time.Duration {
  195. return s.jwtLifeTime
  196. }
  197. func (s Service) JWTCookie() string {
  198. return s.jwtCookie
  199. }
  200. func (s Service) BasicAuthUser() string {
  201. return s.basicAuthUser
  202. }
  203. func (s Service) BasicAuthPassword() string {
  204. return s.basicAuthPassword
  205. }
  206. func (s Service) StaticVersion() uint16 {
  207. return s.staticVersion
  208. }
  209. func (s Service) LoggerStdoutEnabled() bool {
  210. return s.loggerStdoutEnabled
  211. }
  212. func (s Service) LoggerStdoutLevel() string {
  213. return s.loggerStdoutLevel
  214. }
  215. func (s Service) LoggerFileEnabled() bool {
  216. return s.loggerFileEnabled
  217. }
  218. func (s Service) LoggerFileLevel() string {
  219. return s.loggerFileLevel
  220. }
  221. func (s Service) LoggerFilePath() string {
  222. return s.loggerFilePath
  223. }
  224. func (s Service) LoggerEmailEnabled() bool {
  225. return s.loggerEmailEnabled
  226. }
  227. func (s Service) LoggerEmailLevel() string {
  228. return s.loggerEmailLevel
  229. }
  230. func (s Service) LoggerEmailSubject() string {
  231. return s.loggerEmailSubject
  232. }
  233. func (s Service) LoggerEmailRecipient() string {
  234. return s.loggerEmailRecipient
  235. }
  236. func (s Service) LoginRateLimiterMaxRequests() uint16 {
  237. return s.loginRateLimiterMaxRequests
  238. }
  239. func (s Service) LoginRateLimiterExpiration() time.Duration {
  240. return s.loginRateLimiterExpiration
  241. }