env.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package env
  2. import (
  3. "flag"
  4. "github.com/dmitriygnatenko/internal/interfaces"
  5. "github.com/spf13/viper"
  6. )
  7. const defaultConfigPath = "../../.env"
  8. type env struct {
  9. AppPort string `mapstructure:"APP_PORT"`
  10. DBHost string `mapstructure:"DB_HOST"`
  11. DBPort string `mapstructure:"DB_PORT"`
  12. DBName string `mapstructure:"DB_NAME"`
  13. DBUser string `mapstructure:"DB_USER"`
  14. DBPassword string `mapstructure:"DB_PASSWORD"`
  15. DBMaxOpenConns int `mapstructure:"DB_MAX_OPEN_CONNS"`
  16. DBMaxIdleConns int `mapstructure:"DB_MAX_IDLE_CONNS"`
  17. DBMaxConnLifetime int `mapstructure:"DB_MAX_CONN_LIFETIME"`
  18. DBMaxIdleConnLifetime int `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
  19. CORSAllowOrigins string `mapstructure:"CORS_ALLOW_ORIGING"`
  20. CORSAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
  21. JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
  22. JWTLifeTime int `mapstructure:"JWT_LIFETIME"`
  23. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  24. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  25. SMTPHost string `mapstructure:"SMTP_HOST"`
  26. SMTPPort string `mapstructure:"SMTP_PORT"`
  27. SMTPUser string `mapstructure:"SMTP_USER"`
  28. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  29. ErrorsEmail string `mapstructure:"ERRORS_EMAIL"`
  30. GAKey string `mapstructure:"GA_KEY"`
  31. StaticVersion int `mapstructure:"STATIC_VERSION"`
  32. }
  33. func Init() (interfaces.Env, error) {
  34. var configPath string
  35. flag.StringVar(&configPath, "config", "", "Path to .env config file")
  36. flag.Parse()
  37. if configPath == "" {
  38. configPath = defaultConfigPath
  39. }
  40. viper.SetConfigFile(configPath)
  41. viper.SetConfigType("env")
  42. viper.AutomaticEnv()
  43. if err := viper.ReadInConfig(); err != nil {
  44. return nil, err
  45. }
  46. res := &env{}
  47. err := viper.Unmarshal(&res)
  48. return res, err
  49. }
  50. func (e *env) GetAppPort() string {
  51. return e.AppPort
  52. }
  53. func (e *env) GetDBHost() string {
  54. return e.DBHost
  55. }
  56. func (e *env) GetDBPort() string {
  57. return e.DBPort
  58. }
  59. func (e *env) GetDBName() string {
  60. return e.DBName
  61. }
  62. func (e *env) GetDBUser() string {
  63. return e.DBUser
  64. }
  65. func (e *env) GetDBPassword() string {
  66. return e.DBPassword
  67. }
  68. func (e *env) GetCORSAllowOrigins() string {
  69. return e.CORSAllowOrigins
  70. }
  71. func (e *env) GetCORSAllowMethods() string {
  72. return e.CORSAllowMethods
  73. }
  74. func (e *env) GetDBMaxOpenConns() int {
  75. return e.DBMaxOpenConns
  76. }
  77. func (e *env) GetDBMaxIdleConns() int {
  78. return e.DBMaxIdleConns
  79. }
  80. func (e *env) GetDBMaxConnLifetime() int {
  81. return e.DBMaxConnLifetime
  82. }
  83. func (e *env) GetDBMaxIdleConnLifetime() int {
  84. return e.DBMaxIdleConnLifetime
  85. }
  86. func (e *env) GetSMTPHost() string {
  87. return e.SMTPHost
  88. }
  89. func (e *env) GetSMTPPort() string {
  90. return e.SMTPPort
  91. }
  92. func (e *env) GetSMTPUser() string {
  93. return e.SMTPUser
  94. }
  95. func (e *env) GetSMTPPassword() string {
  96. return e.SMTPPassword
  97. }
  98. func (e *env) GetJWTSecretKey() string {
  99. return e.JWTSecretKey
  100. }
  101. func (e *env) GetJWTLifetime() int {
  102. return e.JWTLifeTime
  103. }
  104. func (e *env) GetErrorsEmail() string {
  105. return e.ErrorsEmail
  106. }
  107. func (e *env) GetBasicAuthUser() string {
  108. return e.BasicAuthUser
  109. }
  110. func (e *env) GetBasicAuthPassword() string {
  111. return e.BasicAuthPassword
  112. }
  113. func (e *env) GetStaticVersion() int {
  114. return e.StaticVersion
  115. }
  116. func (e *env) GetGAKey() string {
  117. return e.GAKey
  118. }