-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
105 lines (83 loc) · 2.18 KB
/
status.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
package stgin
import (
"net/http"
)
type Status struct {
StatusCode int
Entity ResponseEntity
Headers http.Header
cookies []*http.Cookie
}
func (status Status) isRedirection() bool {
return status.StatusCode >= 300 && status.StatusCode < 400
}
func (status Status) WithCookies(cookies ...*http.Cookie) Status {
status.cookies = append(status.cookies, cookies...)
return status
}
func (status Status) WithHeaders(headers http.Header) Status {
for key, value := range headers {
status.Headers[key] = value
}
return status
}
var emptyHeaders http.Header = make(map[string][]string, 6)
func CreateResponse(statusCode int, body ResponseEntity) Status {
return Status{
StatusCode: statusCode,
Entity: body,
Headers: emptyHeaders,
}
}
// 2xx Statuses here
func Ok(body ResponseEntity) Status {
return CreateResponse(http.StatusOK, body)
}
func Created(body ResponseEntity) Status {
return CreateResponse(http.StatusCreated, body)
}
// ------------------
// 3xx statuses here
func MovedPermanently(location string) Status {
return Status{
StatusCode: http.StatusMovedPermanently,
Entity: Text(location),
Headers: emptyHeaders,
}
}
func Found(location string) Status {
return Status{
StatusCode: http.StatusFound,
Entity: Text(location),
Headers: emptyHeaders,
}
}
func PermanentRedirect(location string) Status {
return Status{
StatusCode: http.StatusPermanentRedirect,
Entity: Text(location),
Headers: emptyHeaders,
}
}
// ------------------
// 4xx statuses here
func BadRequest(body ResponseEntity) Status {
return CreateResponse(http.StatusBadRequest, body)
}
func Unauthorized(body ResponseEntity) Status {
return CreateResponse(http.StatusUnauthorized, body)
}
func Forbidden(body ResponseEntity) Status {
return CreateResponse(http.StatusForbidden, body)
}
func NotFound(body ResponseEntity) Status {
return CreateResponse(http.StatusNotFound, body)
}
func MethodNotAllowed(body ResponseEntity) Status {
return CreateResponse(http.StatusMethodNotAllowed, body)
}
// ------------------
// 5xx statuses here
func InternalServerError(body ResponseEntity) Status {
return CreateResponse(http.StatusInternalServerError, body)
}