env.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. jwtCookie string
  23. basicAuthUser string
  24. basicAuthPassword string
  25. smtpHost string
  26. smtpPort string
  27. smtpUser string
  28. smtpPassword string
  29. errorsEmail string
  30. gaKey string
  31. staticVersion int
  32. }
  33. func Init() (*Service, 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. s := struct {
  47. AppPort string `mapstructure:"APP_PORT"`
  48. DBHost string `mapstructure:"DB_HOST"`
  49. DBPort string `mapstructure:"DB_PORT"`
  50. DBName string `mapstructure:"DB_NAME"`
  51. DBUser string `mapstructure:"DB_USER"`
  52. DBPassword string `mapstructure:"DB_PASSWORD"`
  53. DBMaxOpenConns int `mapstructure:"DB_MAX_OPEN_CONNS"`
  54. DBMaxIdleConns int `mapstructure:"DB_MAX_IDLE_CONNS"`
  55. DBMaxConnLifetime int `mapstructure:"DB_MAX_CONN_LIFETIME"`
  56. DBMaxIdleConnLifetime int `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
  57. CORSAllowOrigins string `mapstructure:"CORS_ALLOW_ORIGING"`
  58. CORSAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
  59. JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
  60. JWTLifeTime int `mapstructure:"JWT_LIFETIME"`
  61. JWTCookie string `mapstructure:"JWT_COOKIE"`
  62. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  63. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  64. SMTPHost string `mapstructure:"SMTP_HOST"`
  65. SMTPPort string `mapstructure:"SMTP_PORT"`
  66. SMTPUser string `mapstructure:"SMTP_USER"`
  67. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  68. ErrorsEmail string `mapstructure:"ERRORS_EMAIL"`
  69. GAKey string `mapstructure:"GA_KEY"`
  70. StaticVersion int `mapstructure:"STATIC_VERSION"`
  71. }{}
  72. if err := viper.Unmarshal(&s); err != nil {
  73. return nil, err
  74. }
  75. return &Service{
  76. appPort: s.AppPort,
  77. dbHost: s.DBHost,
  78. dbPort: s.DBPort,
  79. dbName: s.DBName,
  80. dbUser: s.DBUser,
  81. dbPassword: s.DBPassword,
  82. dbMaxOpenConns: s.DBMaxOpenConns,
  83. dbMaxIdleConns: s.DBMaxIdleConns,
  84. dbMaxConnLifetime: s.DBMaxConnLifetime,
  85. dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
  86. corsAllowOrigins: s.CORSAllowOrigins,
  87. corsAllowMethods: s.CORSAllowMethods,
  88. jwtSecretKey: s.JWTSecretKey,
  89. jwtLifeTime: s.JWTLifeTime,
  90. jwtCookie: s.JWTCookie,
  91. basicAuthUser: s.BasicAuthUser,
  92. basicAuthPassword: s.BasicAuthPassword,
  93. smtpHost: s.SMTPHost,
  94. smtpPort: s.SMTPPort,
  95. smtpUser: s.SMTPUser,
  96. smtpPassword: s.SMTPPassword,
  97. errorsEmail: s.ErrorsEmail,
  98. gaKey: s.GAKey,
  99. staticVersion: s.StaticVersion,
  100. }, nil
  101. }
  102. func (e *Service) AppPort() string {
  103. return e.appPort
  104. }
  105. func (e *Service) DBHost() string {
  106. return e.dbHost
  107. }
  108. func (e *Service) DBPort() string {
  109. return e.dbPort
  110. }
  111. func (e *Service) DBName() string {
  112. return e.dbName
  113. }
  114. func (e *Service) DBUser() string {
  115. return e.dbUser
  116. }
  117. func (e *Service) DBPassword() string {
  118. return e.dbPassword
  119. }
  120. func (e *Service) CORSAllowOrigins() string {
  121. return e.corsAllowOrigins
  122. }
  123. func (e *Service) CORSAllowMethods() string {
  124. return e.corsAllowMethods
  125. }
  126. func (e *Service) DBMaxOpenConns() int {
  127. return e.dbMaxOpenConns
  128. }
  129. func (e *Service) DBMaxIdleConns() int {
  130. return e.dbMaxIdleConns
  131. }
  132. func (e *Service) DBMaxConnLifetime() int {
  133. return e.dbMaxConnLifetime
  134. }
  135. func (e *Service) DBMaxIdleConnLifetime() int {
  136. return e.dbMaxIdleConnLifetime
  137. }
  138. func (e *Service) SMTPHost() string {
  139. return e.smtpHost
  140. }
  141. func (e *Service) SMTPPort() string {
  142. return e.smtpPort
  143. }
  144. func (e *Service) SMTPUser() string {
  145. return e.smtpUser
  146. }
  147. func (e *Service) SMTPPassword() string {
  148. return e.smtpPassword
  149. }
  150. func (e *Service) JWTSecretKey() string {
  151. return e.jwtSecretKey
  152. }
  153. func (e *Service) JWTCookie() string {
  154. return e.jwtCookie
  155. }
  156. func (e *Service) JWTLifetime() int {
  157. return e.jwtLifeTime
  158. }
  159. func (e *Service) ErrorsEmail() string {
  160. return e.errorsEmail
  161. }
  162. func (e *Service) BasicAuthUser() string {
  163. return e.basicAuthUser
  164. }
  165. func (e *Service) BasicAuthPassword() string {
  166. return e.basicAuthPassword
  167. }
  168. func (e *Service) StaticVersion() int {
  169. return e.staticVersion
  170. }
  171. func (e *Service) GAKey() string {
  172. return e.gaKey
  173. }