summaryrefslogtreecommitdiff
path: root/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'handler.go')
-rw-r--r--handler.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/handler.go b/handler.go
index bf6a21d..1c2d6dc 100644
--- a/handler.go
+++ b/handler.go
@@ -64,3 +64,20 @@ func Filter(
})
}
}
+
+// VirtualHosts builds a handler which dispatches to site handlers by hostname.
+//
+// The 'catchall' argument may be used to specify a handler for use when no hostname
+// match is found. If the catchall is nil and no match is found, the VirtualHosts
+// handler returns a nil response.
+func VirtualHosts(hosts map[string]Handler, catchall Handler) Handler {
+ return HandlerFunc(func(ctx context.Context, request *Request) *Response {
+ if h := hosts[request.Hostname()]; h != nil {
+ return h.Handle(ctx, request)
+ }
+ if catchall != nil {
+ return catchall.Handle(ctx, request)
+ }
+ return nil
+ })
+}