config.go 6.2 KB

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