config.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2018 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 // import "miniflux.app/config"
  5. import (
  6. "net/url"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "miniflux.app/logger"
  11. )
  12. const (
  13. defaultBaseURL = "http://localhost"
  14. defaultDatabaseURL = "user=postgres password=postgres dbname=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. defaultOAuth2ClientID = ""
  28. defaultOAuth2ClientSecret = ""
  29. defaultOAuth2RedirectURL = ""
  30. defaultOAuth2Provider = ""
  31. )
  32. // Config manages configuration parameters.
  33. type Config struct {
  34. IsHTTPS bool
  35. baseURL string
  36. rootURL string
  37. basePath string
  38. }
  39. func (c *Config) parseBaseURL() {
  40. baseURL := os.Getenv("BASE_URL")
  41. if baseURL == "" {
  42. return
  43. }
  44. if baseURL[len(baseURL)-1:] == "/" {
  45. baseURL = baseURL[:len(baseURL)-1]
  46. }
  47. u, err := url.Parse(baseURL)
  48. if err != nil {
  49. logger.Error("Invalid BASE_URL: %v", err)
  50. return
  51. }
  52. scheme := strings.ToLower(u.Scheme)
  53. if scheme != "https" && scheme != "http" {
  54. logger.Error("Invalid BASE_URL: scheme must be http or https")
  55. return
  56. }
  57. c.baseURL = baseURL
  58. c.basePath = u.Path
  59. u.Path = ""
  60. c.rootURL = u.String()
  61. }
  62. // HasDebugMode returns true if debug mode is enabled.
  63. func (c *Config) HasDebugMode() bool {
  64. return getBooleanValue("DEBUG")
  65. }
  66. // BaseURL returns the application base URL with path.
  67. func (c *Config) BaseURL() string {
  68. return c.baseURL
  69. }
  70. // RootURL returns the base URL without path.
  71. func (c *Config) RootURL() string {
  72. return c.rootURL
  73. }
  74. // BasePath returns the application base path according to the base URL.
  75. func (c *Config) BasePath() string {
  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. // DatabaseMaxConns returns the maximum number of database connections.
  90. func (c *Config) DatabaseMaxConns() int {
  91. return getIntValue("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
  92. }
  93. // DatabaseMinConns returns the minimum number of database connections.
  94. func (c *Config) DatabaseMinConns() int {
  95. return getIntValue("DATABASE_MIN_CONNS", defaultDatabaseMinConns)
  96. }
  97. // ListenAddr returns the listen address for the HTTP server.
  98. func (c *Config) ListenAddr() string {
  99. if port := os.Getenv("PORT"); port != "" {
  100. return ":" + port
  101. }
  102. return getStringValue("LISTEN_ADDR", defaultListenAddr)
  103. }
  104. // CertFile returns the SSL certificate filename if any.
  105. func (c *Config) CertFile() string {
  106. return getStringValue("CERT_FILE", defaultCertFile)
  107. }
  108. // KeyFile returns the private key filename for custom SSL certificate.
  109. func (c *Config) KeyFile() string {
  110. return getStringValue("KEY_FILE", defaultKeyFile)
  111. }
  112. // CertDomain returns the domain to use for Let's Encrypt certificate.
  113. func (c *Config) CertDomain() string {
  114. return getStringValue("CERT_DOMAIN", defaultCertDomain)
  115. }
  116. // CertCache returns the directory to use for Let's Encrypt session cache.
  117. func (c *Config) CertCache() string {
  118. return getStringValue("CERT_CACHE", defaultCertCache)
  119. }
  120. // CleanupFrequency returns the interval for cleanup jobs.
  121. func (c *Config) CleanupFrequency() int {
  122. return getIntValue("CLEANUP_FREQUENCY", defaultCleanupFrequency)
  123. }
  124. // WorkerPoolSize returns the number of background worker.
  125. func (c *Config) WorkerPoolSize() int {
  126. return getIntValue("WORKER_POOL_SIZE", defaultWorkerPoolSize)
  127. }
  128. // PollingFrequency returns the interval to refresh feeds in the background.
  129. func (c *Config) PollingFrequency() int {
  130. return getIntValue("POLLING_FREQUENCY", defaultPollingFrequency)
  131. }
  132. // BatchSize returns the number of feeds to send for background processing.
  133. func (c *Config) BatchSize() int {
  134. return getIntValue("BATCH_SIZE", defaultBatchSize)
  135. }
  136. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
  137. func (c *Config) IsOAuth2UserCreationAllowed() bool {
  138. return getBooleanValue("OAUTH2_USER_CREATION")
  139. }
  140. // OAuth2ClientID returns the OAuth2 Client ID.
  141. func (c *Config) OAuth2ClientID() string {
  142. return getStringValue("OAUTH2_CLIENT_ID", defaultOAuth2ClientID)
  143. }
  144. // OAuth2ClientSecret returns the OAuth2 client secret.
  145. func (c *Config) OAuth2ClientSecret() string {
  146. return getStringValue("OAUTH2_CLIENT_SECRET", defaultOAuth2ClientSecret)
  147. }
  148. // OAuth2RedirectURL returns the OAuth2 redirect URL.
  149. func (c *Config) OAuth2RedirectURL() string {
  150. return getStringValue("OAUTH2_REDIRECT_URL", defaultOAuth2RedirectURL)
  151. }
  152. // OAuth2Provider returns the name of the OAuth2 provider configured.
  153. func (c *Config) OAuth2Provider() string {
  154. return getStringValue("OAUTH2_PROVIDER", defaultOAuth2Provider)
  155. }
  156. // HasHSTS returns true if HTTP Strict Transport Security is enabled.
  157. func (c *Config) HasHSTS() bool {
  158. return !getBooleanValue("DISABLE_HSTS")
  159. }
  160. // RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty.
  161. func (c *Config) RunMigrations() bool {
  162. return getBooleanValue("RUN_MIGRATIONS")
  163. }
  164. // CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty.
  165. func (c *Config) CreateAdmin() bool {
  166. return getBooleanValue("CREATE_ADMIN")
  167. }
  168. // PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable.
  169. func (c *Config) PocketConsumerKey(defaultValue string) string {
  170. return getStringValue("POCKET_CONSUMER_KEY", defaultValue)
  171. }
  172. // ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy.
  173. func (c *Config) ProxyImages() string {
  174. return getStringValue("PROXY_IMAGES", defaultProxyImages)
  175. }
  176. // HasHTTPService returns true if the HTTP service is enabled.
  177. func (c *Config) HasHTTPService() bool {
  178. return !getBooleanValue("DISABLE_HTTP_SERVICE")
  179. }
  180. // HasSchedulerService returns true if the scheduler service is enabled.
  181. func (c *Config) HasSchedulerService() bool {
  182. return !getBooleanValue("DISABLE_SCHEDULER_SERVICE")
  183. }
  184. // NewConfig returns a new Config.
  185. func NewConfig() *Config {
  186. cfg := &Config{
  187. baseURL: defaultBaseURL,
  188. rootURL: defaultBaseURL,
  189. IsHTTPS: getBooleanValue("HTTPS"),
  190. }
  191. cfg.parseBaseURL()
  192. return cfg
  193. }
  194. func getBooleanValue(key string) bool {
  195. value := strings.ToLower(os.Getenv(key))
  196. if value == "1" || value == "yes" || value == "true" || value == "on" {
  197. return true
  198. }
  199. return false
  200. }
  201. func getStringValue(key, fallback string) string {
  202. value := os.Getenv(key)
  203. if value == "" {
  204. return fallback
  205. }
  206. return value
  207. }
  208. func getIntValue(key string, fallback int) int {
  209. value := os.Getenv(key)
  210. if value == "" {
  211. return fallback
  212. }
  213. v, err := strconv.Atoi(value)
  214. if err != nil {
  215. return fallback
  216. }
  217. return v
  218. }