aboutsummaryrefslogtreecommitdiffhomepage
path: root/service
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-11-12 10:23:39 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-11-12 10:23:39 -0800
commit5cd7152ae7a136bc29a339aae4e363c1f9e7c1c5 (patch)
tree9cb9ee046977ddafe886b5eaf19993c083d45b86 /service
parent9f85f6703116c3d01d3d1b768df22b466d5d3613 (diff)
Simplify application HTTP middlewares
Diffstat (limited to 'service')
-rw-r--r--service/httpd/httpd.go6
-rw-r--r--service/httpd/middleware.go47
2 files changed, 48 insertions, 5 deletions
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))
+ })
+}