aboutsummaryrefslogtreecommitdiffhomepage
path: root/config/config.go
blob: 1b9e8f853150e47feaa2142bf7f817694c09f242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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"
)

// Default config parameters values
const (
	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
)

// Config manages configuration parameters.
type Config struct {
	IsHTTPS bool
}

// Get returns a config parameter value.
func (c *Config) Get(key, fallback string) string {
	value := os.Getenv(key)
	if value == "" {
		return fallback
	}

	return value
}

// GetInt returns a config parameter as integer.
func (c *Config) GetInt(key string, fallback int) int {
	value := os.Getenv(key)
	if value == "" {
		return fallback
	}

	v, _ := strconv.Atoi(value)
	return v
}

// BaseURL returns the application base URL.
func (c *Config) BaseURL() string {
	return c.Get("BASE_URL", DefaultBaseURL)
}

// DatabaseURL returns the database URL.
func (c *Config) DatabaseURL() string {
	return c.Get("DATABASE_URL", DefaultDatabaseURL)
}

// DatabaseMaxConnections returns the number of maximum database connections.
func (c *Config) DatabaseMaxConnections() int {
	return c.GetInt("DATABASE_MAX_CONNS", DefaultDatabaseMaxConns)
}

// ListenAddr returns the listen address for the HTTP server.
func (c *Config) ListenAddr() string {
	return c.Get("LISTEN_ADDR", DefaultListenAddr)
}

// CertFile returns the SSL certificate filename if any.
func (c *Config) CertFile() string {
	return c.Get("CERT_FILE", DefaultCertFile)
}

// KeyFile returns the private key filename for custom SSL certificate.
func (c *Config) KeyFile() string {
	return c.Get("KEY_FILE", DefaultKeyFile)
}

// CertDomain returns the domain to use for Let's Encrypt certificate.
func (c *Config) CertDomain() string {
	return c.Get("CERT_DOMAIN", DefaultCertDomain)
}

// CertCache returns the directory to use for Let's Encrypt session cache.
func (c *Config) CertCache() string {
	return c.Get("CERT_CACHE", DefaultCertCache)
}

// SessionCleanupFrequency returns the interval for session cleanup.
func (c *Config) SessionCleanupFrequency() int {
	return c.GetInt("SESSION_CLEANUP_FREQUENCY", DefaultSessionCleanupFrequency)
}

// WorkerPoolSize returns the number of background worker.
func (c *Config) WorkerPoolSize() int {
	return c.GetInt("WORKER_POOL_SIZE", DefaultWorkerPoolSize)
}

// PollingFrequency returns the interval to refresh feeds in the background.
func (c *Config) PollingFrequency() int {
	return c.GetInt("POLLING_FREQUENCY", DefaultPollingFrequency)
}

// BatchSize returns the number of feeds to send for background processing.
func (c *Config) BatchSize() int {
	return c.GetInt("BATCH_SIZE", DefaultBatchSize)
}

// NewConfig returns a new Config.
func NewConfig() *Config {
	return &Config{IsHTTPS: os.Getenv("HTTPS") != ""}
}