diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
| commit | ca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch) | |
| tree | deaabaf15d6d91079a68f247e46070399e4343ee /cmd/server/conditionalMiddleware.go | |
| parent | 97dd660925434be537cd9a49a1d0c893b223e357 (diff) | |
| download | go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.gz go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.xz | |
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'cmd/server/conditionalMiddleware.go')
| -rw-r--r-- | cmd/server/conditionalMiddleware.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/cmd/server/conditionalMiddleware.go b/cmd/server/conditionalMiddleware.go new file mode 100644 index 0000000..60b1398 --- /dev/null +++ b/cmd/server/conditionalMiddleware.go @@ -0,0 +1,18 @@ +package main + +import "net/http" + +type conditionalMiddlewareFunc func(r *http.Request) bool + +// conditionalMiddleware simply uses handler a if condition is true and handler +// b if condition is false. +func conditionalMiddleware(condition conditionalMiddlewareFunc, + a, b http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if condition(r) { + a.ServeHTTP(w, r) + } else { + b.ServeHTTP(w, r) + } + }) +} |
