2018-09-23 18:23:46 -04:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-11-20 00:10:04 -05:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package config // import "miniflux.app/config"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2018-02-03 18:33:17 -05:00
|
|
|
"net/url"
|
2017-11-20 00:10:04 -05:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2018-07-07 17:01:02 -04:00
|
|
|
"strings"
|
2018-02-23 21:26:34 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/logger"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-21 22:37:47 -05:00
|
|
|
const (
|
2018-09-23 18:23:46 -04:00
|
|
|
defaultBaseURL = "http://localhost"
|
|
|
|
defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
|
|
|
|
defaultWorkerPoolSize = 5
|
|
|
|
defaultPollingFrequency = 60
|
|
|
|
defaultBatchSize = 10
|
|
|
|
defaultDatabaseMaxConns = 20
|
|
|
|
defaultDatabaseMinConns = 1
|
|
|
|
defaultListenAddr = "127.0.0.1:8080"
|
|
|
|
defaultCertFile = ""
|
|
|
|
defaultKeyFile = ""
|
|
|
|
defaultCertDomain = ""
|
|
|
|
defaultCertCache = "/tmp/cert_cache"
|
|
|
|
defaultCleanupFrequency = 24
|
|
|
|
defaultProxyImages = "http-only"
|
|
|
|
defaultOAuth2ClientID = ""
|
|
|
|
defaultOAuth2ClientSecret = ""
|
|
|
|
defaultOAuth2RedirectURL = ""
|
|
|
|
defaultOAuth2Provider = ""
|
2017-11-21 22:37:47 -05:00
|
|
|
)
|
|
|
|
|
2017-11-21 23:20:20 -05:00
|
|
|
// Config manages configuration parameters.
|
2017-12-28 23:34:18 -05:00
|
|
|
type Config struct {
|
2018-02-03 18:33:17 -05:00
|
|
|
IsHTTPS bool
|
|
|
|
baseURL string
|
|
|
|
rootURL string
|
|
|
|
basePath string
|
2017-12-28 23:34:18 -05:00
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-07-07 17:01:02 -04:00
|
|
|
func (c *Config) parseBaseURL() {
|
|
|
|
baseURL := os.Getenv("BASE_URL")
|
|
|
|
if baseURL == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseURL[len(baseURL)-1:] == "/" {
|
|
|
|
baseURL = baseURL[:len(baseURL)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Invalid BASE_URL: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme := strings.ToLower(u.Scheme)
|
|
|
|
if scheme != "https" && scheme != "http" {
|
|
|
|
logger.Error("Invalid BASE_URL: scheme must be http or https")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.baseURL = baseURL
|
|
|
|
c.basePath = u.Path
|
|
|
|
|
|
|
|
u.Path = ""
|
|
|
|
c.rootURL = u.String()
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:54:38 -05:00
|
|
|
// HasDebugMode returns true if debug mode is enabled.
|
|
|
|
func (c *Config) HasDebugMode() bool {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getBooleanValue("DEBUG")
|
2018-01-29 23:54:38 -05:00
|
|
|
}
|
|
|
|
|
2018-02-03 18:33:17 -05:00
|
|
|
// BaseURL returns the application base URL with path.
|
2018-01-15 21:08:30 -05:00
|
|
|
func (c *Config) BaseURL() string {
|
2018-02-03 18:33:17 -05:00
|
|
|
return c.baseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootURL returns the base URL without path.
|
|
|
|
func (c *Config) RootURL() string {
|
|
|
|
return c.rootURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasePath returns the application base path according to the base URL.
|
|
|
|
func (c *Config) BasePath() string {
|
|
|
|
return c.basePath
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseURL returns the database URL.
|
|
|
|
func (c *Config) DatabaseURL() string {
|
2018-02-23 21:26:34 -05:00
|
|
|
value, exists := os.LookupEnv("DATABASE_URL")
|
|
|
|
if !exists {
|
|
|
|
logger.Info("The environment variable DATABASE_URL is not configured (the default value is used instead)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
value = defaultDatabaseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:28:45 -04:00
|
|
|
// DatabaseMaxConns returns the maximum number of database connections.
|
|
|
|
func (c *Config) DatabaseMaxConns() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:28:45 -04:00
|
|
|
// DatabaseMinConns returns the minimum number of database connections.
|
|
|
|
func (c *Config) DatabaseMinConns() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("DATABASE_MIN_CONNS", defaultDatabaseMinConns)
|
2018-08-01 23:28:45 -04:00
|
|
|
}
|
|
|
|
|
2018-01-15 21:08:30 -05:00
|
|
|
// ListenAddr returns the listen address for the HTTP server.
|
|
|
|
func (c *Config) ListenAddr() string {
|
2018-04-15 23:27:10 -04:00
|
|
|
if port := os.Getenv("PORT"); port != "" {
|
|
|
|
return ":" + port
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("LISTEN_ADDR", defaultListenAddr)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertFile returns the SSL certificate filename if any.
|
|
|
|
func (c *Config) CertFile() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("CERT_FILE", defaultCertFile)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyFile returns the private key filename for custom SSL certificate.
|
|
|
|
func (c *Config) KeyFile() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("KEY_FILE", defaultKeyFile)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertDomain returns the domain to use for Let's Encrypt certificate.
|
|
|
|
func (c *Config) CertDomain() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("CERT_DOMAIN", defaultCertDomain)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertCache returns the directory to use for Let's Encrypt session cache.
|
|
|
|
func (c *Config) CertCache() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("CERT_CACHE", defaultCertCache)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
2018-05-19 19:40:24 -04:00
|
|
|
// CleanupFrequency returns the interval for cleanup jobs.
|
|
|
|
func (c *Config) CleanupFrequency() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("CLEANUP_FREQUENCY", defaultCleanupFrequency)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WorkerPoolSize returns the number of background worker.
|
|
|
|
func (c *Config) WorkerPoolSize() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("WORKER_POOL_SIZE", defaultWorkerPoolSize)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PollingFrequency returns the interval to refresh feeds in the background.
|
|
|
|
func (c *Config) PollingFrequency() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("POLLING_FREQUENCY", defaultPollingFrequency)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BatchSize returns the number of feeds to send for background processing.
|
|
|
|
func (c *Config) BatchSize() int {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getIntValue("BATCH_SIZE", defaultBatchSize)
|
2018-01-20 19:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
|
|
|
|
func (c *Config) IsOAuth2UserCreationAllowed() bool {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getBooleanValue("OAUTH2_USER_CREATION")
|
2018-01-20 19:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2ClientID returns the OAuth2 Client ID.
|
|
|
|
func (c *Config) OAuth2ClientID() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("OAUTH2_CLIENT_ID", defaultOAuth2ClientID)
|
2018-01-20 19:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2ClientSecret returns the OAuth2 client secret.
|
|
|
|
func (c *Config) OAuth2ClientSecret() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("OAUTH2_CLIENT_SECRET", defaultOAuth2ClientSecret)
|
2018-01-20 19:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2RedirectURL returns the OAuth2 redirect URL.
|
|
|
|
func (c *Config) OAuth2RedirectURL() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("OAUTH2_REDIRECT_URL", defaultOAuth2RedirectURL)
|
2018-01-20 19:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2Provider returns the name of the OAuth2 provider configured.
|
|
|
|
func (c *Config) OAuth2Provider() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("OAUTH2_PROVIDER", defaultOAuth2Provider)
|
2018-01-15 21:08:30 -05:00
|
|
|
}
|
|
|
|
|
2018-02-03 18:54:02 -05:00
|
|
|
// HasHSTS returns true if HTTP Strict Transport Security is enabled.
|
|
|
|
func (c *Config) HasHSTS() bool {
|
2018-09-23 18:23:46 -04:00
|
|
|
return !getBooleanValue("DISABLE_HSTS")
|
2018-02-03 18:54:02 -05:00
|
|
|
}
|
|
|
|
|
2018-04-15 23:27:10 -04:00
|
|
|
// RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty.
|
|
|
|
func (c *Config) RunMigrations() bool {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getBooleanValue("RUN_MIGRATIONS")
|
2018-04-15 23:27:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty.
|
|
|
|
func (c *Config) CreateAdmin() bool {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getBooleanValue("CREATE_ADMIN")
|
2018-04-15 23:27:10 -04:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:19:56 -04:00
|
|
|
// PocketConsumerKey returns the Pocket Consumer Key if defined as environment variable.
|
|
|
|
func (c *Config) PocketConsumerKey(defaultValue string) string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("POCKET_CONSUMER_KEY", defaultValue)
|
2018-05-21 15:19:56 -04:00
|
|
|
}
|
|
|
|
|
2018-07-12 20:41:09 -04:00
|
|
|
// ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy.
|
|
|
|
func (c *Config) ProxyImages() string {
|
2018-09-23 18:23:46 -04:00
|
|
|
return getStringValue("PROXY_IMAGES", defaultProxyImages)
|
2018-07-12 20:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:20:20 -05:00
|
|
|
// NewConfig returns a new Config.
|
2017-11-20 00:10:04 -05:00
|
|
|
func NewConfig() *Config {
|
2018-07-07 17:01:02 -04:00
|
|
|
cfg := &Config{
|
|
|
|
baseURL: defaultBaseURL,
|
|
|
|
rootURL: defaultBaseURL,
|
2018-09-23 18:23:46 -04:00
|
|
|
IsHTTPS: getBooleanValue("HTTPS"),
|
2018-07-07 17:01:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.parseBaseURL()
|
|
|
|
return cfg
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2018-09-23 18:23:46 -04:00
|
|
|
|
|
|
|
func getBooleanValue(key string) bool {
|
|
|
|
value := strings.ToLower(os.Getenv(key))
|
|
|
|
if value == "1" || value == "yes" || value == "true" || value == "on" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStringValue(key, fallback string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIntValue(key string, fallback int) int {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|