env.go 7.7 KB

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