From 69b85589c61449d2a5453de1a8eba84d330c159a Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Wed, 14 Jul 2021 13:52:57 -0400 Subject: [PATCH] middleware.PageRoute (#639) --- middleware/page_route.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 middleware/page_route.go diff --git a/middleware/page_route.go b/middleware/page_route.go new file mode 100644 index 00000000..d362e424 --- /dev/null +++ b/middleware/page_route.go @@ -0,0 +1,20 @@ +package middleware + +import ( + "net/http" + "strings" +) + +// PageRoute is a simple middleware which allows you to route a static GET request +// at the middleware stack level. +func PageRoute(path string, handler http.Handler) func(http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" && strings.EqualFold(r.URL.Path, path) { + handler.ServeHTTP(w, r) + return + } + h.ServeHTTP(w, r) + }) + } +}