From 14ae59561cb5cf7e6b38fe4cf9036b899fc56407 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Apr 2021 16:50:02 +0200 Subject: [PATCH] feat: encapsulate request handling into adapters --- handler.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/handler.go b/handler.go index e85fe49..d91f086 100644 --- a/handler.go +++ b/handler.go @@ -12,8 +12,17 @@ import ( // Handler wraps the builder to handle requests type Handler Builder -// ServeHTTP implements http.Handler +// ServeHTTP implements http.Handler and wraps it in middlewares (adapters) func (s Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var h = http.HandlerFunc(s.handleRequest) + + for _, adapter := range s.adapters { + h = adapter(h) + } + h(w, r) +} + +func (s Handler) handleRequest(w http.ResponseWriter, r *http.Request) { defer func() { if rc := recover(); rc != nil { log.Printf("recovering request: %s\n", rc)