env.go 3.0 KB

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