env.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package env
  2. import (
  3. "bufio"
  4. "flag"
  5. "os"
  6. "strings"
  7. "github.com/dmitriygnatenko/internal/interfaces"
  8. )
  9. // nolint:gosec
  10. const (
  11. envPath = "../../config/.env"
  12. appPortEnv = "APP_PORT"
  13. dbHostEnv = "DB_HOST"
  14. dbPortEnv = "DB_PORT"
  15. dbNameEnv = "DB_NAME"
  16. dbUserEnv = "DB_USER"
  17. dbPasswordEnv = "DB_PASSWORD"
  18. smtpHostEnv = "SMTP_HOST"
  19. smtpPortEnv = "SMTP_PORT"
  20. smtpUserEnv = "SMTP_USER"
  21. smtpPasswordEnv = "SMTP_PASSWORD"
  22. staticVersionEnv = "STATIC_VERSION"
  23. gaEnabledEnv = "GA_ENABLED"
  24. adminEmailEnv = "ADMIN_EMAIL"
  25. adminUserEnv = "ADMIN_USER"
  26. adminPasswordEnv = "ADMIN_PASSWORD"
  27. )
  28. type env struct {
  29. appPort string
  30. dbHost string
  31. dbPort string
  32. dbName string
  33. dbUser string
  34. dbPassword string
  35. smtpHost string
  36. smtpPort string
  37. smtpUser string
  38. smtpPassword string
  39. staticVersion string
  40. gaEnabled bool
  41. adminEmail string
  42. adminUser string
  43. adminPassword string
  44. }
  45. func Init() (interfaces.IEnv, error) {
  46. res := &env{}
  47. path := flag.String("config", envPath, "path to .env config")
  48. flag.Parse()
  49. file, err := os.Open(*path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. defer file.Close()
  54. scanner := bufio.NewScanner(file)
  55. for scanner.Scan() {
  56. line := strings.TrimSpace(scanner.Text())
  57. if line == "" {
  58. continue
  59. }
  60. if strings.HasPrefix(line, "#") {
  61. continue
  62. }
  63. parts := strings.Split(line, "=")
  64. if len(parts) < 2 {
  65. continue
  66. }
  67. set(res, parts[0], parts[1])
  68. }
  69. if err = scanner.Err(); err != nil {
  70. return nil, err
  71. }
  72. return res, err
  73. }
  74. func set(res *env, key string, value string) {
  75. switch key {
  76. case appPortEnv:
  77. res.appPort = value
  78. case dbHostEnv:
  79. res.dbHost = value
  80. case dbPortEnv:
  81. res.dbPort = value
  82. case dbNameEnv:
  83. res.dbName = value
  84. case dbUserEnv:
  85. res.dbUser = value
  86. case dbPasswordEnv:
  87. res.dbPassword = value
  88. case smtpPortEnv:
  89. res.smtpPort = value
  90. case smtpHostEnv:
  91. res.smtpHost = value
  92. case smtpUserEnv:
  93. res.smtpUser = value
  94. case smtpPasswordEnv:
  95. res.smtpPassword = value
  96. case staticVersionEnv:
  97. res.staticVersion = value
  98. case gaEnabledEnv:
  99. res.gaEnabled = value == "true"
  100. case adminEmailEnv:
  101. res.adminEmail = value
  102. case adminUserEnv:
  103. res.adminUser = value
  104. case adminPasswordEnv:
  105. res.adminPassword = value
  106. }
  107. }
  108. func (e *env) GetAppPort() string {
  109. return e.appPort
  110. }
  111. func (e *env) GetDBHost() string {
  112. return e.dbHost
  113. }
  114. func (e *env) GetDBPort() string {
  115. return e.dbPort
  116. }
  117. func (e *env) GetDBName() string {
  118. return e.dbName
  119. }
  120. func (e *env) GetDBUser() string {
  121. return e.dbUser
  122. }
  123. func (e *env) GetDBPassword() string {
  124. return e.dbPassword
  125. }
  126. func (e *env) GetSMTPPort() string {
  127. return e.smtpPort
  128. }
  129. func (e *env) GetSMTPHost() string {
  130. return e.smtpHost
  131. }
  132. func (e *env) GetSMTPUser() string {
  133. return e.smtpUser
  134. }
  135. func (e *env) GetSMTPPassword() string {
  136. return e.smtpPassword
  137. }
  138. func (e *env) GetStaticVersion() string {
  139. return e.staticVersion
  140. }
  141. func (e *env) IsGAEnabled() bool {
  142. return e.gaEnabled
  143. }
  144. func (e *env) GetAdminEmail() string {
  145. return e.adminEmail
  146. }
  147. func (e *env) GetAdminUser() string {
  148. return e.adminUser
  149. }
  150. func (e *env) GetAdminPassword() string {
  151. return e.adminPassword
  152. }