config.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package config
  5. import (
  6. "net/url"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/miniflux/miniflux/logger"
  11. )
  12. const (
  13. defaultBaseURL = "http://localhost"
  14. defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
  15. defaultWorkerPoolSize = 5
  16. defaultPollingFrequency = 60
  17. defaultBatchSize = 10
  18. defaultDatabaseMaxConns = 20
  19. defaultListenAddr = "127.0.0.1:8080"
  20. defaultCertFile = ""
  21. defaultKeyFile = ""
  22. defaultCertDomain = ""
  23. defaultCertCache = "/tmp/cert_cache"
  24. defaultCleanupFrequency = 24
  25. defaultProxyImages = "http-only"
  26. )
  27. // Config manages configuration parameters.
  28. type Config struct {
  29. IsHTTPS bool
  30. baseURL string
  31. rootURL string
  32. basePath string
  33. }
  34. func (c *Config) get(key, fallback string) string {
  35. value := os.Getenv(key)
  36. if value == "" {
  37. return fallback
  38. }
  39. return value
  40. }
  41. func (c *Config) getInt(key string, fallback int) int {
  42. value := os.Getenv(key)
  43. if value == "" {
  44. return fallback
  45. }
  46. v, _ := strconv.Atoi(value)
  47. return v
  48. }
  49. func (c *Config) parseBaseURL() {
  50. baseURL := os.Getenv("BASE_URL")
  51. if baseURL == "" {
  52. return
  53. }
  54. if baseURL[len(baseURL)-1:] == "/" {
  55. baseURL = baseURL[:len(baseURL)-1]
  56. }
  57. u, err := url.Parse(baseURL)
  58. if err != nil {
  59. logger.Error("Invalid BASE_URL: %v", err)
  60. return
  61. }
  62. scheme := strings.ToLower(u.Scheme)
  63. if scheme != "https" && scheme != "http" {
  64. logger.Error("Invalid BASE_URL: scheme must be http or https")
  65. return
  66. }
  67. c.baseURL = baseURL
  68. c.basePath = u.Path
  69. u.Path = ""
  70. c.rootURL = u.String()
  71. }
  72. // HasDebugMode returns true if debug mode is enabled.
  73. func (c *Config) HasDebugMode() bool {
  74. return c.get("DEBUG", "") != ""
  75. }
  76. // BaseURL returns the application base URL with path.
  77. func (c *Config) BaseURL() string {
  78. return c.baseURL
  79. }
  80. // RootURL returns the base URL without path.
  81. func (c *Config) RootURL() string {
  82. return c.rootURL
  83. }
  84. // BasePath returns the application base path according to the base URL.
  85. func (c *Config) BasePath() string {
  86. return c.basePath
  87. }
  88. // DatabaseURL returns the database URL.
  89. func (c *Config) DatabaseURL() string {
  90. value, exists := os.LookupEnv("DATABASE_URL")
  91. if !exists {
  92. logger.Info("The environment variable DATABASE_URL is not configured (the default value is used instead)")
  93. }
  94. if value == "" {
  95. value = defaultDatabaseURL
  96. }
  97. return value
  98. }
  99. // DatabaseMaxConnections returns the number of maximum database connections.
  100. func (c *Config) DatabaseMaxConnections() int {
  101. return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
  102. }
  103. // ListenAddr returns the listen address for the HTTP server.
  104. func (c *Config) ListenAddr() string {
  105. if port := os.Getenv("PORT"); port != "" {
  106. return ":" + port
  107. }
  108. return c.get("LISTEN_ADDR", defaultListenAddr)
  109. }
  110. // CertFile returns the SSL certificate filename if any.
  111. func (c *Config) CertFile() string {
  112. return c.get("CERT_FILE", defaultCertFile)
  113. }
  114. // KeyFile returns the private key filename for custom SSL certificate.
  115. func (c *Config) KeyFile() string {
  116. return c.get("KEY_FILE", defaultKeyFile)
  117. }
  118. // CertDomain returns the domain to use for Let's Encrypt certificate.
  119. func (c *Config) CertDomain() string {
  120. return c.get("CERT_DOMAIN", defaultCertDomain)
  121. }
  122. // CertCache returns the directory to use for Let's Encrypt session cache.
  123. func (c *Config) CertCache() string {
  124. return c.get("CERT_CACHE", defaultCertCache)
  125. }
  126. // CleanupFrequency returns the interval for cleanup jobs.
  127. func (c *Config) CleanupFrequency() int {
  128. return c.getInt("CLEANUP_FREQUENCY", defaultCleanupFrequency)
  129. }
  130. // WorkerPoolSize returns the number of background worker.
  131. func (c *Config) WorkerPoolSize() int {
  132. return c.getInt("WORKER_POOL_SIZE", defaultWorkerPoolSize)
  133. }
  134. // PollingFrequency returns the interval to refresh feeds in the background.
  135. func (c *Config) PollingFrequency() int {
  136. return c.getInt("POLLING_FREQUENCY", defaultPollingFrequency)
  137. }
  138. // BatchSize returns the number of feeds to send for background processing.
  139. func (c *Config) BatchSize() int {
  140. return c.getInt("BATCH_SIZE", defaultBatchSize)
  141. }
  142. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
  143. func (c *Config) IsOAuth2UserCreationAllowed() bool {
  144. return c.getInt("OAUTH2_USER_CREATION", 0) == 1
  145. }
  146. // OAuth2ClientID returns the OAuth2 Client ID.
  147. func (c *Config) OAuth2ClientID() string {
  148. return c.get("OAUTH2_CLIENT_ID", "")
  149. }
  150. // OAuth2ClientSecret returns the OAuth2 client secret.
  151. func (c *Config) OAuth2ClientSecret() string {
  152. return c.get("OAUTH2_CLIENT_SECRET", "")
  153. }
  154. // OAuth2RedirectURL returns the OAuth2 redirect URL.
  155. func (c *Config) OAuth2RedirectURL() string {
  156. return c.get("OAUTH2_REDIRECT_URL", "")
  157. }
  158. // OAuth2Provider returns the name of the OAuth2 provider configured.
  159. func (c *Config) OAuth2Provider() string {
  160. return c.get("OAUTH2_PROVIDER", "")
  161. }
  162. // HasHSTS returns true if HTTP Strict Transport Security is enabled.
  163. func (c *Config) HasHSTS() bool {
  164. return c.get("DISABLE_HSTS", "") == ""
  165. }
  166. // RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty.
  167. func (c *Config) RunMigrations() bool {
  168. return c.get("RUN_MIGRATIONS", "") != ""
  169. }
  170. // CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty.
  171. func (c *Config) CreateAdmin() bool {
  172. return c.get("CREATE_ADMIN", "") != ""
  173. }
  174. // PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable.
  175. func (c *Config) PocketConsumerKey(defaultValue string) string {
  176. return c.get("POCKET_CONSUMER_KEY", defaultValue)
  177. }
  178. // ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy.
  179. func (c *Config) ProxyImages() string {
  180. return c.get("PROXY_IMAGES", defaultProxyImages)
  181. }
  182. // NewConfig returns a new Config.
  183. func NewConfig() *Config {
  184. cfg := &Config{
  185. baseURL: defaultBaseURL,
  186. rootURL: defaultBaseURL,
  187. IsHTTPS: os.Getenv("HTTPS") != "",
  188. }
  189. cfg.parseBaseURL()
  190. return cfg
  191. }