-
Notifications
You must be signed in to change notification settings - Fork 56
/
multiplexHandler.go
53 lines (44 loc) · 1.04 KB
/
multiplexHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package serverHandler
import (
"../param"
"net/http"
)
type aliasWithHandler struct {
alias alias
handler http.Handler
}
type multiplexHandler struct {
aliasWithHandlers []aliasWithHandler
}
func (mux multiplexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, ah := range mux.aliasWithHandlers {
if ah.alias.isMatch(r.URL.Path) || ah.alias.isPredecessorOf(r.URL.Path) {
ah.handler.ServeHTTP(w, r)
return
}
}
defaultHandler.ServeHTTP(w, r)
}
func newMultiplexHandler(
p *param.Param,
ap *aliasParam,
) http.Handler {
if len(p.Aliases) == 0 {
return defaultHandler
}
aliases := newAliases(p.Aliases)
if len(aliases) == 1 {
alias, hasRootAlias := aliases.byUrlPath("/")
if hasRootAlias {
return newAliasHandler(p, ap, alias, aliases)
}
}
aliasWithHandlers := make([]aliasWithHandler, len(aliases))
for i, alias := range aliases {
aliasWithHandlers[i] = aliasWithHandler{
alias: alias,
handler: newAliasHandler(p, ap, alias, aliases),
}
}
return multiplexHandler{aliasWithHandlers}
}