config.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package config
  2. import (
  3. "flag"
  4. "time"
  5. "github.com/spf13/viper"
  6. )
  7. const defaultConfigPath = "../../.env"
  8. type Service struct {
  9. appPort string
  10. dbDriver string
  11. dbHost string
  12. dbPort uint16
  13. dbName string
  14. dbUser string
  15. dbPassword string
  16. dbMaxOpenConns uint16
  17. dbMaxIdleConns uint16
  18. dbMaxOpenConnLifetime time.Duration
  19. dbMaxIdleConnLifetime time.Duration
  20. dbSSLMode string
  21. corsAllowOrigins string
  22. corsAllowMethods string
  23. jwtSecretKey string
  24. jwtLifeTime time.Duration
  25. basicAuthUser string
  26. basicAuthPassword string
  27. smtpHost string
  28. smtpPort uint16
  29. smtpUser string
  30. smtpPassword string
  31. loggerStdoutEnabled bool
  32. loggerStdoutLevel string
  33. loggerStdoutAddSource bool
  34. loggerEmailEnabled bool
  35. loggerEmailLevel string
  36. loggerEmailAddSource bool
  37. loggerEmailRecipient string
  38. loggerEmailSubject string
  39. }
  40. func Init() (*Service, error) {
  41. var configPath string
  42. flag.StringVar(&configPath, "config", "", "Path to .env config file")
  43. flag.Parse()
  44. if configPath == "" {
  45. configPath = defaultConfigPath
  46. }
  47. viper.SetConfigFile(configPath)
  48. viper.SetConfigType("env")
  49. viper.AutomaticEnv()
  50. if err := viper.ReadInConfig(); err != nil {
  51. return nil, err
  52. }
  53. s := struct {
  54. AppPort string `mapstructure:"APP_PORT"`
  55. DBDriver string `mapstructure:"DB_DRIVER"`
  56. DBHost string `mapstructure:"DB_HOST"`
  57. DBPort uint16 `mapstructure:"DB_PORT"`
  58. DBName string `mapstructure:"DB_NAME"`
  59. DBUser string `mapstructure:"DB_USER"`
  60. DBPassword string `mapstructure:"DB_PASSWORD"`
  61. DBSSLMode string `mapstructure:"DB_SSL_MODE"`
  62. DBMaxOpenConns uint16 `mapstructure:"DB_MAX_OPEN_CONNS"`
  63. DBMaxIdleConns uint16 `mapstructure:"DB_MAX_IDLE_CONNS"`
  64. DBMaxOpenConnLifetime time.Duration `mapstructure:"DB_MAX_OPEN_CONN_LIFETIME"`
  65. DBMaxIdleConnLifetime time.Duration `mapstructure:"DB_MAX_IDLE_CONN_LIFETIME"`
  66. CORSAllowOrigins string `mapstructure:"CORS_ALLOW_ORIGING"`
  67. CORSAllowMethods string `mapstructure:"CORS_ALLOW_METHODS"`
  68. JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
  69. JWTLifeTime time.Duration `mapstructure:"JWT_LIFETIME"`
  70. BasicAuthUser string `mapstructure:"BASIC_AUTH_USER"`
  71. BasicAuthPassword string `mapstructure:"BASIC_AUTH_PASSWORD"`
  72. SMTPHost string `mapstructure:"SMTP_HOST"`
  73. SMTPPort uint16 `mapstructure:"SMTP_PORT"`
  74. SMTPUser string `mapstructure:"SMTP_USER"`
  75. SMTPPassword string `mapstructure:"SMTP_PASSWORD"`
  76. LoggerStdoutEnabled bool `mapstructure:"LOGGER_STDOUT_ENABLED"`
  77. LoggerStdoutLevel string `mapstructure:"LOGGER_STDOUT_LEVEL"`
  78. LoggerStdoutAddSource bool `mapstructure:"LOGGER_STDOUT_ADD_SOURCE"`
  79. LoggerEmailEnabled bool `mapstructure:"LOGGER_EMAIL_ENABLED"`
  80. LoggerEmailLevel string `mapstructure:"LOGGER_EMAIL_LEVEL"`
  81. LoggerEmailAddSource bool `mapstructure:"LOGGER_EMAIL_ADD_SOURCE"`
  82. LoggerEmailRecipient string `mapstructure:"LOGGER_EMAIL_RECIPIENT"`
  83. LoggerEmailSubject string `mapstructure:"LOGGER_EMAIL_SUBJECT"`
  84. }{}
  85. if err := viper.Unmarshal(&s); err != nil {
  86. return nil, err
  87. }
  88. return &Service{
  89. appPort: s.AppPort,
  90. dbDriver: s.DBDriver,
  91. dbSSLMode: s.DBSSLMode,
  92. dbHost: s.DBHost,
  93. dbPort: s.DBPort,
  94. dbName: s.DBName,
  95. dbUser: s.DBUser,
  96. dbPassword: s.DBPassword,
  97. dbMaxOpenConns: s.DBMaxOpenConns,
  98. dbMaxIdleConns: s.DBMaxIdleConns,
  99. dbMaxOpenConnLifetime: s.DBMaxOpenConnLifetime,
  100. dbMaxIdleConnLifetime: s.DBMaxIdleConnLifetime,
  101. corsAllowOrigins: s.CORSAllowOrigins,
  102. corsAllowMethods: s.CORSAllowMethods,
  103. jwtSecretKey: s.JWTSecretKey,
  104. jwtLifeTime: s.JWTLifeTime,
  105. basicAuthUser: s.BasicAuthUser,
  106. basicAuthPassword: s.BasicAuthPassword,
  107. smtpHost: s.SMTPHost,
  108. smtpPort: s.SMTPPort,
  109. smtpUser: s.SMTPUser,
  110. smtpPassword: s.SMTPPassword,
  111. loggerStdoutEnabled: s.LoggerStdoutEnabled,
  112. loggerStdoutLevel: s.LoggerStdoutLevel,
  113. loggerStdoutAddSource: s.LoggerStdoutAddSource,
  114. loggerEmailEnabled: s.LoggerEmailEnabled,
  115. loggerEmailLevel: s.LoggerEmailLevel,
  116. loggerEmailAddSource: s.LoggerEmailAddSource,
  117. loggerEmailRecipient: s.LoggerEmailRecipient,
  118. loggerEmailSubject: s.LoggerEmailSubject,
  119. }, nil
  120. }
  121. func (e *Service) AppPort() string {
  122. return e.appPort
  123. }
  124. func (e *Service) DBDriver() string {
  125. return e.dbDriver
  126. }
  127. func (e *Service) DBSSLMode() string {
  128. return e.dbSSLMode
  129. }
  130. func (e *Service) DBHost() string {
  131. return e.dbHost
  132. }
  133. func (e *Service) DBPort() uint16 {
  134. return e.dbPort
  135. }
  136. func (e *Service) DBName() string {
  137. return e.dbName
  138. }
  139. func (e *Service) DBUser() string {
  140. return e.dbUser
  141. }
  142. func (e *Service) DBPassword() string {
  143. return e.dbPassword
  144. }
  145. func (e *Service) CORSAllowOrigins() string {
  146. return e.corsAllowOrigins
  147. }
  148. func (e *Service) CORSAllowMethods() string {
  149. return e.corsAllowMethods
  150. }
  151. func (e *Service) DBMaxOpenConns() uint16 {
  152. return e.dbMaxOpenConns
  153. }
  154. func (e *Service) DBMaxIdleConns() uint16 {
  155. return e.dbMaxIdleConns
  156. }
  157. func (e *Service) DBMaxOpenConnLifetime() time.Duration {
  158. return e.dbMaxOpenConnLifetime
  159. }
  160. func (e *Service) DBMaxIdleConnLifetime() time.Duration {
  161. return e.dbMaxIdleConnLifetime
  162. }
  163. func (e *Service) SMTPHost() string {
  164. return e.smtpHost
  165. }
  166. func (e *Service) SMTPPort() uint16 {
  167. return e.smtpPort
  168. }
  169. func (e *Service) SMTPUser() string {
  170. return e.smtpUser
  171. }
  172. func (e *Service) SMTPPassword() string {
  173. return e.smtpPassword
  174. }
  175. func (e *Service) JWTSecretKey() string {
  176. return e.jwtSecretKey
  177. }
  178. func (e *Service) JWTLifetime() time.Duration {
  179. return e.jwtLifeTime
  180. }
  181. func (e *Service) BasicAuthUser() string {
  182. return e.basicAuthUser
  183. }
  184. func (e *Service) BasicAuthPassword() string {
  185. return e.basicAuthPassword
  186. }
  187. func (e *Service) LoggerStdoutEnabled() bool {
  188. return e.loggerStdoutEnabled
  189. }
  190. func (e *Service) LoggerStdoutLevel() string {
  191. return e.loggerStdoutLevel
  192. }
  193. func (e *Service) LoggerStdoutAddSource() bool {
  194. return e.loggerStdoutAddSource
  195. }
  196. func (e *Service) LoggerEmailEnabled() bool {
  197. return e.loggerEmailEnabled
  198. }
  199. func (e *Service) LoggerEmailLevel() string {
  200. return e.loggerEmailLevel
  201. }
  202. func (e *Service) LoggerEmailAddSource() bool {
  203. return e.loggerEmailAddSource
  204. }
  205. func (e *Service) LoggerEmailRecipient() string {
  206. return e.loggerEmailRecipient
  207. }
  208. func (e *Service) LoggerEmailSubject() string {
  209. return e.loggerEmailSubject
  210. }