-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
37 lines (31 loc) · 970 Bytes
/
Copy pathhandler.go
File metadata and controls
37 lines (31 loc) · 970 Bytes
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
package routing
import (
"net/http"
"strings"
)
func getAutoMethodOptionsHandler(router *Router) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
availVerbs := getAvailableMethods(router, request)
writer.WriteHeader(http.StatusNoContent)
writer.Header().Set("Allow", strings.Join(availVerbs, ", "))
}
}
func getRedirectHandler(url string, code ...int) http.HandlerFunc {
defaultCode := http.StatusFound
if len(code) > 0 && code[0] >= http.StatusMultipleChoices && code[0] <= http.StatusPermanentRedirect {
defaultCode = code[0]
}
return func(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, url, defaultCode)
}
}
func getAvailableMethods(router *Router, request *http.Request) []string {
availVerbs := make([]string, 0, 9)
for verb, tree := range router.trees {
n := tree.find(request)
if n != nil {
availVerbs = append(availVerbs, verb)
}
}
return availVerbs
}