-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethodmux.go
46 lines (36 loc) · 1001 Bytes
/
methodmux.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
package servemux
import (
"net/http"
)
// MethodHandlers multiplexes HTTP requests by HTTP method.
type MethodHandlers map[string]http.Handler
func (m MethodHandlers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h, found := m[r.Method]
if !found {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
h.ServeHTTP(w, r)
}
// MethodFuncs multiplexes HTTP requests by HTTP method.
type MethodFuncs map[string]func(http.ResponseWriter, *http.Request)
func (m MethodFuncs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h, found := m[r.Method]
if !found {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
h(w, r)
}
// func (m MethodHandlers) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// if len(m) > 0 {
// allowMethods := make([]string, len(m))
// i := 0
// for k := range m {
// allowMethods[i] = k
// i++
// }
// w.Header().Set("Allow", strings.Join(allowMethods, ", "))
// }
// w.WriteHeader(http.StatusMethodNotAllowed)
// }