summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-05-02 08:43:45 -0600
committertjpcc <tjp@ctrl-c.club>2023-05-02 08:43:45 -0600
commitace21031f98f0e7512e39120b5b30b58457efc02 (patch)
tree0ce963fd7002df921e5859f0c5972b3552ab5a3b
parent3d5acb3b68af936d1820770d6094d96c65a9c0d3 (diff)
simple handler dispatcher for hostname-based virtual hosting
-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
+ })
+}