From 5cd7152ae7a136bc29a339aae4e363c1f9e7c1c5 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Mon, 12 Nov 2018 10:23:39 -0800 Subject: Simplify application HTTP middlewares --- middleware/client_ip.go | 21 -------------------- middleware/doc.go | 10 ---------- middleware/header_config.go | 24 ----------------------- middleware/logging.go | 20 ------------------- middleware/middleware.go | 23 ---------------------- service/httpd/httpd.go | 6 +----- service/httpd/middleware.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 48 insertions(+), 103 deletions(-) delete mode 100644 middleware/client_ip.go delete mode 100644 middleware/doc.go delete mode 100644 middleware/header_config.go delete mode 100644 middleware/logging.go delete mode 100644 middleware/middleware.go create mode 100644 service/httpd/middleware.go diff --git a/middleware/client_ip.go b/middleware/client_ip.go deleted file mode 100644 index 853fcc9..0000000 --- a/middleware/client_ip.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2018 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 middleware // import "miniflux.app/middleware" - -import ( - "context" - "net/http" - - "miniflux.app/http/request" -) - -// ClientIP stores in the real client IP address in the context. -func (m *Middleware) ClientIP(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - ctx = context.WithValue(ctx, request.ClientIPContextKey, request.FindClientIP(r)) - next.ServeHTTP(w, r.WithContext(ctx)) - }) -} diff --git a/middleware/doc.go b/middleware/doc.go deleted file mode 100644 index 382ba43..0000000 --- a/middleware/doc.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2018 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 middleware contains application HTTP middlewares. - -*/ -package middleware // import "miniflux.app/middleware" diff --git a/middleware/header_config.go b/middleware/header_config.go deleted file mode 100644 index d62d76c..0000000 --- a/middleware/header_config.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 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 middleware // import "miniflux.app/middleware" - -import ( - "net/http" -) - -// HeaderConfig changes config values according to HTTP headers. -func (m *Middleware) HeaderConfig(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("X-Forwarded-Proto") == "https" { - m.cfg.IsHTTPS = true - } - - if m.cfg.IsHTTPS && m.cfg.HasHSTS() { - w.Header().Set("Strict-Transport-Security", "max-age=31536000") - } - - next.ServeHTTP(w, r) - }) -} diff --git a/middleware/logging.go b/middleware/logging.go deleted file mode 100644 index fdf1ce3..0000000 --- a/middleware/logging.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2018 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 middleware // import "miniflux.app/middleware" - -import ( - "net/http" - - "miniflux.app/http/request" - "miniflux.app/logger" -) - -// Logging logs the HTTP request. -func (m *Middleware) Logging(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - logger.Debug("[HTTP] %s %s %s", request.ClientIP(r), r.Method, r.RequestURI) - next.ServeHTTP(w, r) - }) -} diff --git a/middleware/middleware.go b/middleware/middleware.go deleted file mode 100644 index cf393d2..0000000 --- a/middleware/middleware.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018 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 middleware // import "miniflux.app/middleware" - -import ( - "github.com/gorilla/mux" - "miniflux.app/config" - "miniflux.app/storage" -) - -// Middleware handles different middleware handlers. -type Middleware struct { - cfg *config.Config - store *storage.Storage - router *mux.Router -} - -// New returns a new middleware. -func New(cfg *config.Config, store *storage.Storage, router *mux.Router) *Middleware { - return &Middleware{cfg, store, router} -} diff --git a/service/httpd/httpd.go b/service/httpd/httpd.go index a365353..8e69063 100644 --- a/service/httpd/httpd.go +++ b/service/httpd/httpd.go @@ -16,7 +16,6 @@ import ( "miniflux.app/config" "miniflux.app/fever" "miniflux.app/logger" - "miniflux.app/middleware" "miniflux.app/reader/feed" "miniflux.app/storage" "miniflux.app/ui" @@ -137,15 +136,12 @@ func startHTTPServer(server *http.Server) { func setupHandler(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handler, pool *worker.Pool) *mux.Router { router := mux.NewRouter() - middleware := middleware.New(cfg, store, router) if cfg.BasePath() != "" { router = router.PathPrefix(cfg.BasePath()).Subrouter() } - router.Use(middleware.ClientIP) - router.Use(middleware.HeaderConfig) - router.Use(middleware.Logging) + router.Use(newMiddleware(cfg).Serve) fever.Serve(router, cfg, store) api.Serve(router, store, feedHandler) diff --git a/service/httpd/middleware.go b/service/httpd/middleware.go new file mode 100644 index 0000000..9e5abc3 --- /dev/null +++ b/service/httpd/middleware.go @@ -0,0 +1,47 @@ +// Copyright 2018 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 httpd // import "miniflux.app/service/httpd" + +import ( + "context" + "net/http" + + "miniflux.app/config" + "miniflux.app/http/request" + "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 { + 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 + } + + protocol := "HTTP" + if m.cfg.IsHTTPS { + protocol = "HTTPS" + } + + logger.Debug("[%s] %s %s %s", protocol, clientIP, r.Method, r.RequestURI) + + if m.cfg.IsHTTPS && m.cfg.HasHSTS() { + w.Header().Set("Strict-Transport-Security", "max-age=31536000") + } + + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} -- cgit v1.2.3