From 228862fefaa645026caa483ffe9993bf8c00b22e Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 1 Jun 2019 18:18:09 -0700 Subject: Refactor config package - Parse configuration only once during startup time - Store configuration values in a global variable --- service/httpd/httpd.go | 30 +++++++++++++++--------------- service/httpd/middleware.go | 16 ++++------------ service/scheduler/scheduler.go | 6 +++--- 3 files changed, 22 insertions(+), 30 deletions(-) (limited to 'service') diff --git a/service/httpd/httpd.go b/service/httpd/httpd.go index 2e93086..5139969 100644 --- a/service/httpd/httpd.go +++ b/service/httpd/httpd.go @@ -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")) diff --git a/service/httpd/middleware.go b/service/httpd/middleware.go index 9e5abc3..c169e08 100644 --- a/service/httpd/middleware.go +++ b/service/httpd/middleware.go @@ -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") } diff --git a/service/scheduler/scheduler.go b/service/scheduler/scheduler.go index 184bb49..1d51356 100644 --- a/service/scheduler/scheduler.go +++ b/service/scheduler/scheduler.go @@ -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) { -- cgit v1.2.3