env.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package env
  2. import (
  3. "flag"
  4. "github.com/spf13/viper"
  5. )
  6. const defaultConfigPath = "../../.env"
  7. type Service struct {
  8. appPort string
  9. dbHost string
  10. dbPort string
  11. dbName string
  12. dbUser string
  13. dbPassword string
  14. dbMaxOpenConns int
  15. dbMaxIdleConns int
  16. dbMaxConnLifetime int
  17. dbMaxIdleConnLifetime int
  18. corsAllowOrigins string
  19. corsAllowMethods string
  20. jwtSecretKey string
  21. jwtLifeTime int
  22. basicAuthUser string
  23. basicAuthPassword string
  24. smtpHost string
  25. smtpPort string
  26. smtpUser string
  27. smtpPassword string
  28. errorsEmail string
  29. }
  30. func Init() (*Service, error) {
  31. var configPath string
  32. flag.StringVar(&configPath, "config", "", "Path to .env config file")
  33. flag.Parse()
  34. if configPath == "" {
  35. configPath = defaultConfigPath
  36. }
  37. viper.SetConfigFile(configPath)
  38. viper.SetConfigType("env")
  39. viper.AutomaticEnv()
  40. if err := viper.ReadInConfig(); err != nil {
  41. return nil, err
  42. }
  43. s := struct {
  44. AppPort string `mapstructure:"APP_PORT"`
  45. DBHost string `mapstructure:"DB_HOST"`
  46. DBPort string `mapstructure:"DB_PORT"`
  47. DBName string `mapstructure:"DB_NAME"`
  48. DBUser string `mapstructure:"DB_USER"`
  49. DBPassword string `mapstructure:"DB_PASSWORD"`
  50. DBMaxOpenConns int `mapstructure:"DB_MAX_OPEN_CONNS"`
  51. DBMaxIdleConns int `mapstructure:"DB_MAX_IDLE_CONNS"`
  52. DBMaxConnLifetime int `mapstructure:"DB_MAX_CONN_LIFETIME"`
  53. DBMaxIdleConnLifetime int `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
  54. CORSAllowOrigins string `mapstructure:"CORS_ALLOW_ORIGING"`
  55. CORSAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
  56. JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
  57. JWTLifeTime int `mapstructure:"JWT_LIFETIME"`
  58. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  59. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  60. SMTPHost string `mapstructure:"SMTP_HOST"`
  61. SMTPPort string `mapstructure:"SMTP_PORT"`
  62. SMTPUser string `mapstructure:"SMTP_USER"`
  63. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  64. ErrorsEmail string `mapstructure:"ERRORS_EMAIL"`
  65. }{}
  66. if err := viper.Unmarshal(&s); err != nil {
  67. return nil, err
  68. }
  69. return &Service{
  70. appPort: s.AppPort,
  71. dbHost: s.DBHost,
  72. dbPort: s.DBPort,
  73. dbName: s.DBName,
  74. dbUser: s.DBUser,
  75. dbPassword: s.DBPassword,
  76. dbMaxOpenConns: s.DBMaxOpenConns,
  77. dbMaxIdleConns: s.DBMaxIdleConns,
  78. dbMaxConnLifetime: s.DBMaxConnLifetime,
  79. dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
  80. corsAllowOrigins: s.CORSAllowOrigins,
  81. corsAllowMethods: s.CORSAllowMethods,
  82. jwtSecretKey: s.JWTSecretKey,
  83. jwtLifeTime: s.JWTLifeTime,
  84. basicAuthUser: s.BasicAuthUser,
  85. basicAuthPassword: s.BasicAuthPassword,
  86. smtpHost: s.SMTPHost,
  87. smtpPort: s.SMTPPort,
  88. smtpUser: s.SMTPUser,
  89. smtpPassword: s.SMTPPassword,
  90. errorsEmail: s.ErrorsEmail,
  91. }, nil
  92. }
  93. func (e *Service) GetAppPort() string {
  94. return e.appPort
  95. }
  96. func (e *Service) GetDBHost() string {
  97. return e.dbHost
  98. }
  99. func (e *Service) GetDBPort() string {
  100. return e.dbPort
  101. }
  102. func (e *Service) GetDBName() string {
  103. return e.dbName
  104. }
  105. func (e *Service) GetDBUser() string {
  106. return e.dbUser
  107. }
  108. func (e *Service) GetDBPassword() string {
  109. return e.dbPassword
  110. }
  111. func (e *Service) GetCORSAllowOrigins() string {
  112. return e.corsAllowOrigins
  113. }
  114. func (e *Service) GetCORSAllowMethods() string {
  115. return e.corsAllowMethods
  116. }
  117. func (e *Service) GetDBMaxOpenConns() int {
  118. return e.dbMaxOpenConns
  119. }
  120. func (e *Service) GetDBMaxIdleConns() int {
  121. return e.dbMaxIdleConns
  122. }
  123. func (e *Service) GetDBMaxConnLifetime() int {
  124. return e.dbMaxConnLifetime
  125. }
  126. func (e *Service) GetDBMaxIdleConnLifetime() int {
  127. return e.dbMaxIdleConnLifetime
  128. }
  129. func (e *Service) GetSMTPHost() string {
  130. return e.smtpHost
  131. }
  132. func (e *Service) GetSMTPPort() string {
  133. return e.smtpPort
  134. }
  135. func (e *Service) GetSMTPUser() string {
  136. return e.smtpUser
  137. }
  138. func (e *Service) GetSMTPPassword() string {
  139. return e.smtpPassword
  140. }
  141. func (e *Service) GetJWTSecretKey() string {
  142. return e.jwtSecretKey
  143. }
  144. func (e *Service) GetJWTLifetime() int {
  145. return e.jwtLifeTime
  146. }
  147. func (e *Service) GetErrorsEmail() string {
  148. return e.errorsEmail
  149. }
  150. func (e *Service) GetBasicAuthUser() string {
  151. return e.basicAuthUser
  152. }
  153. func (e *Service) GetBasicAuthPassword() string {
  154. return e.basicAuthPassword
  155. }