mirror of
https://github.com/miniflux/v2.git
synced 2025-09-15 18:57:04 +00:00
Refactor config package
- Parse configuration only once during startup time - Store configuration values in a global variable
This commit is contained in:
parent
04d85b3c63
commit
228862fefa
28 changed files with 922 additions and 624 deletions
|
@ -27,17 +27,17 @@ import (
|
|||
)
|
||||
|
||||
// Serve starts a new HTTP server.
|
||||
func Serve(cfg *config.Config, store *storage.Storage, pool *worker.Pool, feedHandler *feed.Handler) *http.Server {
|
||||
certFile := cfg.CertFile()
|
||||
keyFile := cfg.KeyFile()
|
||||
certDomain := cfg.CertDomain()
|
||||
certCache := cfg.CertCache()
|
||||
listenAddr := cfg.ListenAddr()
|
||||
func Serve(store *storage.Storage, pool *worker.Pool, feedHandler *feed.Handler) *http.Server {
|
||||
certFile := config.Opts.CertFile()
|
||||
keyFile := config.Opts.CertKeyFile()
|
||||
certDomain := config.Opts.CertDomain()
|
||||
certCache := config.Opts.CertCache()
|
||||
listenAddr := config.Opts.ListenAddr()
|
||||
server := &http.Server{
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
Handler: setupHandler(cfg, store, feedHandler, pool),
|
||||
Handler: setupHandler(store, feedHandler, pool),
|
||||
}
|
||||
|
||||
switch {
|
||||
|
@ -46,10 +46,10 @@ func Serve(cfg *config.Config, store *storage.Storage, pool *worker.Pool, feedHa
|
|||
case strings.HasPrefix(listenAddr, "/"):
|
||||
startUnixSocketServer(server, listenAddr)
|
||||
case certDomain != "" && certCache != "":
|
||||
cfg.IsHTTPS = true
|
||||
config.Opts.HTTPS = true
|
||||
startAutoCertTLSServer(server, certDomain, certCache)
|
||||
case certFile != "" && keyFile != "":
|
||||
cfg.IsHTTPS = true
|
||||
config.Opts.HTTPS = true
|
||||
server.Addr = listenAddr
|
||||
startTLSServer(server, certFile, keyFile)
|
||||
default:
|
||||
|
@ -156,18 +156,18 @@ func startHTTPServer(server *http.Server) {
|
|||
}()
|
||||
}
|
||||
|
||||
func setupHandler(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handler, pool *worker.Pool) *mux.Router {
|
||||
func setupHandler(store *storage.Storage, feedHandler *feed.Handler, pool *worker.Pool) *mux.Router {
|
||||
router := mux.NewRouter()
|
||||
|
||||
if cfg.BasePath() != "" {
|
||||
router = router.PathPrefix(cfg.BasePath()).Subrouter()
|
||||
if config.Opts.BasePath() != "" {
|
||||
router = router.PathPrefix(config.Opts.BasePath()).Subrouter()
|
||||
}
|
||||
|
||||
router.Use(newMiddleware(cfg).Serve)
|
||||
router.Use(middleware)
|
||||
|
||||
fever.Serve(router, cfg, store)
|
||||
fever.Serve(router, store)
|
||||
api.Serve(router, store, feedHandler)
|
||||
ui.Serve(router, cfg, store, pool, feedHandler)
|
||||
ui.Serve(router, store, pool, feedHandler)
|
||||
|
||||
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
|
|
|
@ -13,32 +13,24 @@ import (
|
|||
"miniflux.app/logger"
|
||||
)
|
||||
|
||||
type middleware struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func newMiddleware(cfg *config.Config) *middleware {
|
||||
return &middleware{cfg}
|
||||
}
|
||||
|
||||
func (m *middleware) Serve(next http.Handler) http.Handler {
|
||||
func middleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
clientIP := request.FindClientIP(r)
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, request.ClientIPContextKey, clientIP)
|
||||
|
||||
if r.Header.Get("X-Forwarded-Proto") == "https" {
|
||||
m.cfg.IsHTTPS = true
|
||||
config.Opts.HTTPS = true
|
||||
}
|
||||
|
||||
protocol := "HTTP"
|
||||
if m.cfg.IsHTTPS {
|
||||
if config.Opts.HTTPS {
|
||||
protocol = "HTTPS"
|
||||
}
|
||||
|
||||
logger.Debug("[%s] %s %s %s", protocol, clientIP, r.Method, r.RequestURI)
|
||||
|
||||
if m.cfg.IsHTTPS && m.cfg.HasHSTS() {
|
||||
if config.Opts.HTTPS && config.Opts.HasHSTS() {
|
||||
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
|
||||
}
|
||||
|
||||
|
|
|
@ -14,10 +14,10 @@ import (
|
|||
)
|
||||
|
||||
// Serve starts the internal scheduler.
|
||||
func Serve(cfg *config.Config, store *storage.Storage, pool *worker.Pool) {
|
||||
func Serve(store *storage.Storage, pool *worker.Pool) {
|
||||
logger.Info(`Starting scheduler...`)
|
||||
go feedScheduler(store, pool, cfg.PollingFrequency(), cfg.BatchSize())
|
||||
go cleanupScheduler(store, cfg.CleanupFrequency(), cfg.ArchiveReadDays())
|
||||
go feedScheduler(store, pool, config.Opts.PollingFrequency(), config.Opts.BatchSize())
|
||||
go cleanupScheduler(store, config.Opts.CleanupFrequency(), config.Opts.ArchiveReadDays())
|
||||
}
|
||||
|
||||
func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSize int) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue