2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2017-11-22 04:37:47 +01:00
|
|
|
const (
|
2018-01-21 01:49:47 +01:00
|
|
|
defaultBaseURL = "http://localhost"
|
|
|
|
defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
|
|
|
|
defaultWorkerPoolSize = 5
|
|
|
|
defaultPollingFrequency = 60
|
|
|
|
defaultBatchSize = 10
|
|
|
|
defaultDatabaseMaxConns = 20
|
|
|
|
defaultListenAddr = "127.0.0.1:8080"
|
|
|
|
defaultCertFile = ""
|
|
|
|
defaultKeyFile = ""
|
|
|
|
defaultCertDomain = ""
|
|
|
|
defaultCertCache = "/tmp/cert_cache"
|
|
|
|
defaultSessionCleanupFrequency = 24
|
2017-11-22 04:37:47 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 05:20:20 +01:00
|
|
|
// Config manages configuration parameters.
|
2017-12-29 05:34:18 +01:00
|
|
|
type Config struct {
|
|
|
|
IsHTTPS bool
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-01-21 01:49:47 +01:00
|
|
|
func (c *Config) get(key, fallback string) string {
|
2017-11-20 06:10:04 +01:00
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2018-01-21 01:49:47 +01:00
|
|
|
func (c *Config) getInt(key string, fallback int) int {
|
2017-11-20 06:10:04 +01:00
|
|
|
value := os.Getenv(key)
|
|
|
|
if value == "" {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := strconv.Atoi(value)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:08:30 +01:00
|
|
|
// BaseURL returns the application base URL.
|
|
|
|
func (c *Config) BaseURL() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("BASE_URL", defaultBaseURL)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseURL returns the database URL.
|
|
|
|
func (c *Config) DatabaseURL() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("DATABASE_URL", defaultDatabaseURL)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DatabaseMaxConnections returns the number of maximum database connections.
|
|
|
|
func (c *Config) DatabaseMaxConnections() int {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAddr returns the listen address for the HTTP server.
|
|
|
|
func (c *Config) ListenAddr() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("LISTEN_ADDR", defaultListenAddr)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertFile returns the SSL certificate filename if any.
|
|
|
|
func (c *Config) CertFile() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("CERT_FILE", defaultCertFile)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyFile returns the private key filename for custom SSL certificate.
|
|
|
|
func (c *Config) KeyFile() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("KEY_FILE", defaultKeyFile)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertDomain returns the domain to use for Let's Encrypt certificate.
|
|
|
|
func (c *Config) CertDomain() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("CERT_DOMAIN", defaultCertDomain)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertCache returns the directory to use for Let's Encrypt session cache.
|
|
|
|
func (c *Config) CertCache() string {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.get("CERT_CACHE", defaultCertCache)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionCleanupFrequency returns the interval for session cleanup.
|
|
|
|
func (c *Config) SessionCleanupFrequency() int {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.getInt("SESSION_CLEANUP_FREQUENCY", defaultSessionCleanupFrequency)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WorkerPoolSize returns the number of background worker.
|
|
|
|
func (c *Config) WorkerPoolSize() int {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.getInt("WORKER_POOL_SIZE", defaultWorkerPoolSize)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PollingFrequency returns the interval to refresh feeds in the background.
|
|
|
|
func (c *Config) PollingFrequency() int {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.getInt("POLLING_FREQUENCY", defaultPollingFrequency)
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BatchSize returns the number of feeds to send for background processing.
|
|
|
|
func (c *Config) BatchSize() int {
|
2018-01-21 01:49:47 +01:00
|
|
|
return c.getInt("BATCH_SIZE", defaultBatchSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
|
|
|
|
func (c *Config) IsOAuth2UserCreationAllowed() bool {
|
|
|
|
return c.getInt("OAUTH2_USER_CREATION", 0) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2ClientID returns the OAuth2 Client ID.
|
|
|
|
func (c *Config) OAuth2ClientID() string {
|
|
|
|
return c.get("OAUTH2_CLIENT_ID", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2ClientSecret returns the OAuth2 client secret.
|
|
|
|
func (c *Config) OAuth2ClientSecret() string {
|
|
|
|
return c.get("OAUTH2_CLIENT_SECRET", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2RedirectURL returns the OAuth2 redirect URL.
|
|
|
|
func (c *Config) OAuth2RedirectURL() string {
|
|
|
|
return c.get("OAUTH2_REDIRECT_URL", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2Provider returns the name of the OAuth2 provider configured.
|
|
|
|
func (c *Config) OAuth2Provider() string {
|
|
|
|
return c.get("OAUTH2_PROVIDER", "")
|
2018-01-16 03:08:30 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 05:20:20 +01:00
|
|
|
// NewConfig returns a new Config.
|
2017-11-20 06:10:04 +01:00
|
|
|
func NewConfig() *Config {
|
2017-12-29 05:34:18 +01:00
|
|
|
return &Config{IsHTTPS: os.Getenv("HTTPS") != ""}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|