forked from stretchr/goweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
46 lines (38 loc) · 1.54 KB
/
helpers.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 goweb
import (
"github.com/stretchr/goweb/paths"
"net/http"
)
const (
// headerKeyLocation is the header key for Location.
headerKeyLocation string = "Location"
)
// Status writes the header with the specified status.
func Status(response http.ResponseWriter, status int) {
response.WriteHeader(status)
}
// RedirectTo writes the Location header.
func RedirectTo(response http.ResponseWriter, pathOrURLSegments ...interface{}) {
response.Header().Set(headerKeyLocation, paths.PathFromSegments(pathOrURLSegments...))
}
// redirectWithStatus writes the Location header and sets the specified
// status in the response.
func redirectWithStatus(response http.ResponseWriter, status int, pathOrURLSegments ...interface{}) {
RedirectTo(response, pathOrURLSegments...)
Status(response, status)
}
// Redirect writes the Location header and sets the http.StatusFound
// response.
func Redirect(response http.ResponseWriter, pathOrURLSegments ...interface{}) {
redirectWithStatus(response, http.StatusFound, pathOrURLSegments...)
}
// RedirectTemp writes the Location header and sets the http.StatusTemporaryRedirect
// response.
func RedirectTemp(response http.ResponseWriter, pathOrURLSegments ...interface{}) {
redirectWithStatus(response, http.StatusTemporaryRedirect, pathOrURLSegments...)
}
// RedirectPerm writes the Location header and sets the http.StatusMovedPermanently
// response.
func RedirectPerm(response http.ResponseWriter, pathOrURLSegments ...interface{}) {
redirectWithStatus(response, http.StatusMovedPermanently, pathOrURLSegments...)
}