env.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package env
  2. import (
  3. "flag"
  4. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/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. JWTCookie string `mapstructure:"JWT_COOKIE"`
  24. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  25. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  26. SMTPHost string `mapstructure:"SMTP_HOST"`
  27. SMTPPort string `mapstructure:"SMTP_PORT"`
  28. SMTPUser string `mapstructure:"SMTP_USER"`
  29. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  30. ErrorsEmail string `mapstructure:"ERRORS_EMAIL"`
  31. GAKey string `mapstructure:"GA_KEY"`
  32. StaticVersion int `mapstructure:"STATIC_VERSION"`
  33. }
  34. func Init() (interfaces.Env, error) {
  35. var configPath string
  36. flag.StringVar(&configPath, "config", "", "Path to .env config file")
  37. flag.Parse()
  38. if configPath == "" {
  39. configPath = defaultConfigPath
  40. }
  41. viper.SetConfigFile(configPath)
  42. viper.SetConfigType("env")
  43. viper.AutomaticEnv()
  44. if err := viper.ReadInConfig(); err != nil {
  45. return nil, err
  46. }
  47. res := &env{}
  48. err := viper.Unmarshal(&res)
  49. return res, err
  50. }
  51. func (e *env) GetAppPort() string {
  52. return e.AppPort
  53. }
  54. func (e *env) GetDBHost() string {
  55. return e.DBHost
  56. }
  57. func (e *env) GetDBPort() string {
  58. return e.DBPort
  59. }
  60. func (e *env) GetDBName() string {
  61. return e.DBName
  62. }
  63. func (e *env) GetDBUser() string {
  64. return e.DBUser
  65. }
  66. func (e *env) GetDBPassword() string {
  67. return e.DBPassword
  68. }
  69. func (e *env) GetCORSAllowOrigins() string {
  70. return e.CORSAllowOrigins
  71. }
  72. func (e *env) GetCORSAllowMethods() string {
  73. return e.CORSAllowMethods
  74. }
  75. func (e *env) GetDBMaxOpenConns() int {
  76. return e.DBMaxOpenConns
  77. }
  78. func (e *env) GetDBMaxIdleConns() int {
  79. return e.DBMaxIdleConns
  80. }
  81. func (e *env) GetDBMaxConnLifetime() int {
  82. return e.DBMaxConnLifetime
  83. }
  84. func (e *env) GetDBMaxIdleConnLifetime() int {
  85. return e.DBMaxIdleConnLifetime
  86. }
  87. func (e *env) GetSMTPHost() string {
  88. return e.SMTPHost
  89. }
  90. func (e *env) GetSMTPPort() string {
  91. return e.SMTPPort
  92. }
  93. func (e *env) GetSMTPUser() string {
  94. return e.SMTPUser
  95. }
  96. func (e *env) GetSMTPPassword() string {
  97. return e.SMTPPassword
  98. }
  99. func (e *env) GetJWTSecretKey() string {
  100. return e.JWTSecretKey
  101. }
  102. func (e *env) GetJWTCookie() string {
  103. return e.JWTCookie
  104. }
  105. func (e *env) GetJWTLifetime() int {
  106. return e.JWTLifeTime
  107. }
  108. func (e *env) GetErrorsEmail() string {
  109. return e.ErrorsEmail
  110. }
  111. func (e *env) GetBasicAuthUser() string {
  112. return e.BasicAuthUser
  113. }
  114. func (e *env) GetBasicAuthPassword() string {
  115. return e.BasicAuthPassword
  116. }
  117. func (e *env) GetStaticVersion() int {
  118. return e.StaticVersion
  119. }
  120. func (e *env) GetGAKey() string {
  121. return e.GAKey
  122. }