1
0
Fork 0

Add functions to get config values

This commit is contained in:
Frédéric Guillot 2018-01-15 18:08:30 -08:00
parent d7f66ffa5c
commit c2fd2e747a
5 changed files with 72 additions and 12 deletions

View file

@ -26,8 +26,8 @@ func Parse() {
cfg := config.NewConfig()
store := storage.NewStorage(
cfg.Get("DATABASE_URL", config.DefaultDatabaseURL),
cfg.GetInt("DATABASE_MAX_CONNS", config.DefaultDatabaseMaxConns),
cfg.DatabaseURL(),
cfg.DatabaseMaxConnections(),
)
if *flagInfo {

View file

@ -51,6 +51,66 @@ func (c *Config) GetInt(key string, fallback int) int {
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") != ""}

View file

@ -27,17 +27,17 @@ func Run(cfg *config.Config, store *storage.Storage) {
signal.Notify(stop, syscall.SIGTERM)
feedHandler := feed.NewFeedHandler(store)
pool := scheduler.NewWorkerPool(feedHandler, cfg.GetInt("WORKER_POOL_SIZE", config.DefaultWorkerPoolSize))
pool := scheduler.NewWorkerPool(feedHandler, cfg.WorkerPoolSize())
server := newServer(cfg, store, pool, feedHandler)
scheduler.NewFeedScheduler(
store,
pool,
cfg.GetInt("POLLING_FREQUENCY", config.DefaultPollingFrequency),
cfg.GetInt("BATCH_SIZE", config.DefaultBatchSize),
cfg.PollingFrequency(),
cfg.BatchSize(),
)
scheduler.NewSessionScheduler(store, config.DefaultSessionCleanupFrequency)
scheduler.NewSessionScheduler(store, cfg.SessionCleanupFrequency())
<-stop
logger.Info("Shutting down the server...")

View file

@ -19,15 +19,15 @@ import (
)
func newServer(cfg *config.Config, store *storage.Storage, pool *scheduler.WorkerPool, feedHandler *feed.Handler) *http.Server {
certFile := cfg.Get("CERT_FILE", config.DefaultCertFile)
keyFile := cfg.Get("KEY_FILE", config.DefaultKeyFile)
certDomain := cfg.Get("CERT_DOMAIN", config.DefaultCertDomain)
certCache := cfg.Get("CERT_CACHE", config.DefaultCertCache)
certFile := cfg.CertFile()
keyFile := cfg.KeyFile()
certDomain := cfg.CertDomain()
certCache := cfg.CertCache()
server := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
Addr: cfg.Get("LISTEN_ADDR", config.DefaultListenAddr),
Addr: cfg.ListenAddr(),
Handler: routes(cfg, store, feedHandler, pool),
}

View file

@ -36,7 +36,7 @@ type Engine struct {
func (e *Engine) parseAll() {
funcMap := template.FuncMap{
"baseURL": func() string {
return e.cfg.Get("BASE_URL", config.DefaultBaseURL)
return e.cfg.BaseURL()
},
"hasOAuth2Provider": func(provider string) bool {
return e.cfg.Get("OAUTH2_PROVIDER", "") == provider