feat: encapsulate request handling into adapters
This commit is contained in:
parent
96164127e1
commit
14ae59561c
11
handler.go
11
handler.go
|
@ -12,8 +12,17 @@ import (
|
||||||
// Handler wraps the builder to handle requests
|
// Handler wraps the builder to handle requests
|
||||||
type Handler Builder
|
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) {
|
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() {
|
defer func() {
|
||||||
if rc := recover(); rc != nil {
|
if rc := recover(); rc != nil {
|
||||||
log.Printf("recovering request: %s\n", rc)
|
log.Printf("recovering request: %s\n", rc)
|
||||||
|
|
Loading…
Reference in New Issue