config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "github.com/miniflux/miniflux/logger"
  10. )
  11. const (
  12. defaultBaseURL = "http://localhost"
  13. defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
  14. defaultWorkerPoolSize = 5
  15. defaultPollingFrequency = 60
  16. defaultBatchSize = 10
  17. defaultDatabaseMaxConns = 20
  18. defaultListenAddr = "127.0.0.1:8080"
  19. defaultCertFile = ""
  20. defaultKeyFile = ""
  21. defaultCertDomain = ""
  22. defaultCertCache = "/tmp/cert_cache"
  23. defaultSessionCleanupFrequency = 24
  24. )
  25. // Config manages configuration parameters.
  26. type Config struct {
  27. IsHTTPS bool
  28. baseURL string
  29. rootURL string
  30. basePath string
  31. }
  32. func (c *Config) get(key, fallback string) string {
  33. value := os.Getenv(key)
  34. if value == "" {
  35. return fallback
  36. }
  37. return value
  38. }
  39. func (c *Config) getInt(key string, fallback int) int {
  40. value := os.Getenv(key)
  41. if value == "" {
  42. return fallback
  43. }
  44. v, _ := strconv.Atoi(value)
  45. return v
  46. }
  47. // HasDebugMode returns true if debug mode is enabled.
  48. func (c *Config) HasDebugMode() bool {
  49. return c.get("DEBUG", "") != ""
  50. }
  51. // BaseURL returns the application base URL with path.
  52. func (c *Config) BaseURL() string {
  53. if c.baseURL == "" {
  54. c.baseURL = c.get("BASE_URL", defaultBaseURL)
  55. if c.baseURL[len(c.baseURL)-1:] == "/" {
  56. c.baseURL = c.baseURL[:len(c.baseURL)-1]
  57. }
  58. }
  59. return c.baseURL
  60. }
  61. // RootURL returns the base URL without path.
  62. func (c *Config) RootURL() string {
  63. if c.rootURL == "" {
  64. u, _ := url.Parse(c.BaseURL())
  65. u.Path = ""
  66. c.rootURL = u.String()
  67. }
  68. return c.rootURL
  69. }
  70. // BasePath returns the application base path according to the base URL.
  71. func (c *Config) BasePath() string {
  72. if c.basePath == "" {
  73. u, _ := url.Parse(c.BaseURL())
  74. c.basePath = u.Path
  75. }
  76. return c.basePath
  77. }
  78. // DatabaseURL returns the database URL.
  79. func (c *Config) DatabaseURL() string {
  80. value, exists := os.LookupEnv("DATABASE_URL")
  81. if !exists {
  82. logger.Info("The environment variable DATABASE_URL is not configured (the default value is used instead)")
  83. }
  84. if value == "" {
  85. value = defaultDatabaseURL
  86. }
  87. return value
  88. }
  89. // DatabaseMaxConnections returns the number of maximum database connections.
  90. func (c *Config) DatabaseMaxConnections() int {
  91. return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
  92. }
  93. // ListenAddr returns the listen address for the HTTP server.
  94. func (c *Config) ListenAddr() string {
  95. return c.get("LISTEN_ADDR", defaultListenAddr)
  96. }
  97. // CertFile returns the SSL certificate filename if any.
  98. func (c *Config) CertFile() string {
  99. return c.get("CERT_FILE", defaultCertFile)
  100. }
  101. // KeyFile returns the private key filename for custom SSL certificate.
  102. func (c *Config) KeyFile() string {
  103. return c.get("KEY_FILE", defaultKeyFile)
  104. }
  105. // CertDomain returns the domain to use for Let's Encrypt certificate.
  106. func (c *Config) CertDomain() string {
  107. return c.get("CERT_DOMAIN", defaultCertDomain)
  108. }
  109. // CertCache returns the directory to use for Let's Encrypt session cache.
  110. func (c *Config) CertCache() string {
  111. return c.get("CERT_CACHE", defaultCertCache)
  112. }
  113. // SessionCleanupFrequency returns the interval for session cleanup.
  114. func (c *Config) SessionCleanupFrequency() int {
  115. return c.getInt("SESSION_CLEANUP_FREQUENCY", defaultSessionCleanupFrequency)
  116. }
  117. // WorkerPoolSize returns the number of background worker.
  118. func (c *Config) WorkerPoolSize() int {
  119. return c.getInt("WORKER_POOL_SIZE", defaultWorkerPoolSize)
  120. }
  121. // PollingFrequency returns the interval to refresh feeds in the background.
  122. func (c *Config) PollingFrequency() int {
  123. return c.getInt("POLLING_FREQUENCY", defaultPollingFrequency)
  124. }
  125. // BatchSize returns the number of feeds to send for background processing.
  126. func (c *Config) BatchSize() int {
  127. return c.getInt("BATCH_SIZE", defaultBatchSize)
  128. }
  129. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
  130. func (c *Config) IsOAuth2UserCreationAllowed() bool {
  131. return c.getInt("OAUTH2_USER_CREATION", 0) == 1
  132. }
  133. // OAuth2ClientID returns the OAuth2 Client ID.
  134. func (c *Config) OAuth2ClientID() string {
  135. return c.get("OAUTH2_CLIENT_ID", "")
  136. }
  137. // OAuth2ClientSecret returns the OAuth2 client secret.
  138. func (c *Config) OAuth2ClientSecret() string {
  139. return c.get("OAUTH2_CLIENT_SECRET", "")
  140. }
  141. // OAuth2RedirectURL returns the OAuth2 redirect URL.
  142. func (c *Config) OAuth2RedirectURL() string {
  143. return c.get("OAUTH2_REDIRECT_URL", "")
  144. }
  145. // OAuth2Provider returns the name of the OAuth2 provider configured.
  146. func (c *Config) OAuth2Provider() string {
  147. return c.get("OAUTH2_PROVIDER", "")
  148. }
  149. // HasHSTS returns true if HTTP Strict Transport Security is enabled.
  150. func (c *Config) HasHSTS() bool {
  151. return c.get("DISABLE_HSTS", "") == ""
  152. }
  153. // NewConfig returns a new Config.
  154. func NewConfig() *Config {
  155. return &Config{IsHTTPS: os.Getenv("HTTPS") != ""}
  156. }