/
mux.go
212 lines (186 loc) · 5.46 KB
/
mux.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2016 JT Olds
// See LICENSE for copying information
// Package whmux provides some useful request mux helpers for demultiplexing
// requests to one of a number of handlers.
package whmux // import "gopkg.in/webhelp.v1/whmux"
import (
"net/http"
"sort"
"strings"
"gopkg.in/webhelp.v1/wherr"
"gopkg.in/webhelp.v1/whroute"
)
// Dir is an http.Handler that mimics a directory. It mutates an incoming
// request's URL.Path to properly namespace handlers. This way a handler can
// assume it has the root of its section. If you want the original URL, use
// req.RequestURI (but don't modify it).
type Dir map[string]http.Handler
// ServeHTTP implements http.handler
func (d Dir) ServeHTTP(w http.ResponseWriter, r *http.Request) {
dir, left := Shift(r.URL.Path)
handler, ok := d[dir]
if !ok {
wherr.Handle(w, r, wherr.NotFound.New("resource: %#v", dir))
return
}
if left == "" {
left = "/"
}
r.URL.Path = left
handler.ServeHTTP(w, r)
}
// Routes implements whroute.Lister
func (d Dir) Routes(
cb func(method, path string, annotations map[string]string)) {
keys := make([]string, 0, len(d))
for element := range d {
keys = append(keys, element)
}
sort.Strings(keys)
for _, element := range keys {
whroute.Routes(d[element],
func(method, path string, annotations map[string]string) {
if element == "" {
cb(method, "/", annotations)
} else {
cb(method, "/"+element+path, annotations)
}
})
}
}
var _ http.Handler = Dir(nil)
var _ whroute.Lister = Dir(nil)
// Shift pulls the first directory out of the path and returns the remainder.
func Shift(path string) (dir, left string) {
// slice off the first "/"s if they exists
path = strings.TrimLeft(path, "/")
if len(path) == 0 {
return "", ""
}
// find the first '/' after the initial one
split := strings.Index(path, "/")
if split == -1 {
return path, ""
}
return path[:split], path[split:]
}
// Method is an http.Handler muxer that keys off of the given HTTP request
// method.
type Method map[string]http.Handler
// ServeHTTP implements http.handler
func (m Method) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if handler, found := m[r.Method]; found {
handler.ServeHTTP(w, r)
return
}
wherr.Handle(w, r,
wherr.MethodNotAllowed.New("bad method: %#v", r.Method))
}
// Routes implements whroute.Lister
func (m Method) Routes(
cb func(method, path string, annotations map[string]string)) {
keys := make([]string, 0, len(m))
for method := range m {
keys = append(keys, method)
}
sort.Strings(keys)
for _, method := range keys {
handler := m[method]
whroute.Routes(handler,
func(_, path string, annotations map[string]string) {
cb(method, path, annotations)
})
}
}
var _ http.Handler = Method(nil)
var _ whroute.Lister = Method(nil)
// ExactPath takes an http.Handler that returns a new http.Handler that doesn't
// accept any more path elements and returns a 404 if more are provided.
func ExactPath(h http.Handler) http.Handler {
return Dir{"": h}
}
// RequireMethod takes an http.Handler and returns a new http.Handler that only
// works with the given HTTP method. If a different method is used, a 405 is
// returned.
func RequireMethod(method string, h http.Handler) http.Handler {
return Method{method: h}
}
// RequireGet is simply RequireMethod but called with "GET" as the first
// argument.
func RequireGet(h http.Handler) http.Handler {
return RequireMethod("GET", h)
}
// Exact is simply RequireGet and ExactPath called together.
func Exact(h http.Handler) http.Handler {
return RequireGet(ExactPath(h))
}
// Host is an http.Handler that chooses a subhandler based on the request
// Host header. The star host ("*") is a default handler.
type Host map[string]http.Handler
// ServeHTTP implements http.handler
func (h Host) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler, ok := h[r.Host]
if !ok {
handler, ok = h["*"]
if !ok {
wherr.Handle(w, r, wherr.NotFound.New("host: %#v", r.Host))
return
}
}
handler.ServeHTTP(w, r)
}
// Routes implements whroute.Lister
func (h Host) Routes(
cb func(method, path string, annotations map[string]string)) {
keys := make([]string, 0, len(h))
for element := range h {
keys = append(keys, element)
}
sort.Strings(keys)
for _, host := range keys {
whroute.Routes(h[host],
func(method, path string, annotations map[string]string) {
cp := make(map[string]string, len(annotations)+1)
for key, val := range annotations {
cp[key] = val
}
cp["Host"] = host
cb(method, path, cp)
})
}
}
var _ http.Handler = Host(nil)
var _ whroute.Lister = Host(nil)
// Overlay is essentially a Dir that you can put in front of another
// http.Handler. If the requested entry isn't in the overlay Dir, the
// Default will be used. If no Default is specified this works exactly the
// same as a Dir.
type Overlay struct {
Default http.Handler
Overlay Dir
}
// ServeHTTP implements http.handler
func (o Overlay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
dir, left := Shift(r.URL.Path)
handler, ok := o.Overlay[dir]
if !ok {
if o.Default == nil {
wherr.Handle(w, r, wherr.NotFound.New("resource: %#v", dir))
return
}
o.Default.ServeHTTP(w, r)
return
}
r.URL.Path = left
handler.ServeHTTP(w, r)
}
// Routes implements whroute.Lister
func (o Overlay) Routes(
cb func(method, path string, annotations map[string]string)) {
whroute.Routes(o.Overlay, cb)
if o.Default != nil {
whroute.Routes(o.Default, cb)
}
}
var _ http.Handler = Overlay{}
var _ whroute.Lister = Overlay{}