diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-05-02 08:43:45 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-05-02 08:43:45 -0600 |
commit | ace21031f98f0e7512e39120b5b30b58457efc02 (patch) | |
tree | 0ce963fd7002df921e5859f0c5972b3552ab5a3b /handler.go | |
parent | 3d5acb3b68af936d1820770d6094d96c65a9c0d3 (diff) |
simple handler dispatcher for hostname-based virtual hosting
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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 + }) +} |