env.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package env
  2. import (
  3. "bufio"
  4. "os"
  5. "strings"
  6. "github.com/dmitriygnatenko/internal/interfaces"
  7. )
  8. // nolint:gosec
  9. const (
  10. envPathVar = "ENV_PATH"
  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 := os.Getenv(envPathVar)
  48. if path == "" {
  49. path = envPath
  50. }
  51. file, err := os.Open(path)
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer file.Close()
  56. scanner := bufio.NewScanner(file)
  57. for scanner.Scan() {
  58. line := strings.TrimSpace(scanner.Text())
  59. if line == "" {
  60. continue
  61. }
  62. if strings.HasPrefix(line, "#") {
  63. continue
  64. }
  65. parts := strings.Split(line, "=")
  66. if len(parts) < 2 {
  67. continue
  68. }
  69. set(res, parts[0], parts[1])
  70. }
  71. if err = scanner.Err(); err != nil {
  72. return nil, err
  73. }
  74. return res, err
  75. }
  76. func set(res *env, key string, value string) {
  77. switch key {
  78. case appPortEnv:
  79. res.appPort = value
  80. case dbHostEnv:
  81. res.dbHost = value
  82. case dbPortEnv:
  83. res.dbPort = value
  84. case dbNameEnv:
  85. res.dbName = value
  86. case dbUserEnv:
  87. res.dbUser = value
  88. case dbPasswordEnv:
  89. res.dbPassword = value
  90. case smtpPortEnv:
  91. res.smtpPort = value
  92. case smtpHostEnv:
  93. res.smtpHost = value
  94. case smtpUserEnv:
  95. res.smtpUser = value
  96. case smtpPasswordEnv:
  97. res.smtpPassword = value
  98. case staticVersionEnv:
  99. res.staticVersion = value
  100. case gaEnabledEnv:
  101. res.gaEnabled = value == "true"
  102. case adminEmailEnv:
  103. res.adminEmail = value
  104. case adminUserEnv:
  105. res.adminUser = value
  106. case adminPasswordEnv:
  107. res.adminPassword = value
  108. }
  109. }
  110. func (e *env) GetAppPort() string {
  111. return e.appPort
  112. }
  113. func (e *env) GetDBHost() string {
  114. return e.dbHost
  115. }
  116. func (e *env) GetDBPort() string {
  117. return e.dbPort
  118. }
  119. func (e *env) GetDBName() string {
  120. return e.dbName
  121. }
  122. func (e *env) GetDBUser() string {
  123. return e.dbUser
  124. }
  125. func (e *env) GetDBPassword() string {
  126. return e.dbPassword
  127. }
  128. func (e *env) GetSMTPPort() string {
  129. return e.smtpPort
  130. }
  131. func (e *env) GetSMTPHost() string {
  132. return e.smtpHost
  133. }
  134. func (e *env) GetSMTPUser() string {
  135. return e.smtpUser
  136. }
  137. func (e *env) GetSMTPPassword() string {
  138. return e.smtpPassword
  139. }
  140. func (e *env) GetStaticVersion() string {
  141. return e.staticVersion
  142. }
  143. func (e *env) IsGAEnabled() bool {
  144. return e.gaEnabled
  145. }
  146. func (e *env) GetAdminEmail() string {
  147. return e.adminEmail
  148. }
  149. func (e *env) GetAdminUser() string {
  150. return e.adminUser
  151. }
  152. func (e *env) GetAdminPassword() string {
  153. return e.adminPassword
  154. }