-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
54 lines (44 loc) · 1.12 KB
/
http.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
package mohttp
import (
"golang.org/x/net/context"
"net/http"
)
func TemporaryRedirect(c context.Context, path string) {
http.Redirect(GetResponseWriter(c), GetRequest(c), path, http.StatusTemporaryRedirect)
}
func TemporaryRedirectHandler(path string) Handler {
return HandlerFunc(func(c context.Context) {
TemporaryRedirect(c, path)
Next(c)
})
}
func PermanentRedirect(c context.Context, path string) {
http.Redirect(GetResponseWriter(c), GetRequest(c), path, http.StatusMovedPermanently)
}
func PermanentRedirectHandler(path string) Handler {
return HandlerFunc(func(c context.Context) {
PermanentRedirect(c, path)
Next(c)
})
}
func Status(c context.Context, code int) {
GetResponseWriter(c).WriteHeader(code)
}
func StatusHandler(code int) Handler {
return HandlerFunc(func(c context.Context) {
Status(c, code)
Next(c)
})
}
func HeadersHandler(pairs ...string) Handler {
l := len(pairs)
if l%2 == 1 {
panic("Header pairs must be a multiple of 2")
}
return HandlerFunc(func(c context.Context) {
for i := 0; i < l/2; i++ {
GetResponseWriter(c).Header().Add(pairs[2*i], pairs[2*i+1])
}
Next(c)
})
}