-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpresponse.go
113 lines (101 loc) · 2.77 KB
/
httpresponse.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
106
107
108
109
110
111
112
113
package gopeach
import (
"strconv"
"time"
)
// Error represents an http error.
type HTTPResponse struct {
Timestamp int64
Status int
Error string
Message string
Path string
}
// NewError returns a new Error.
func NewHTTPResponse() *HTTPResponse {
return &HTTPResponse{}
}
// Created returns a new HTTPResponse with status 201.
func (e *HTTPResponse) Created(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 201
e.Error = ""
e.Message = message
e.Path = path
return e
}
// Accepted returns a new HTTPResponse with status 202.
func (e *HTTPResponse) Accepted(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 202
e.Error = ""
e.Message = message
e.Path = path
return e
}
// BadRequest returns a new HTTPResponse with status 400.
func (e *HTTPResponse) BadRequest(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 400
e.Error = "bad request"
e.Message = message
e.Path = path
return e
}
// Unauthorized returns a new HTTPResponse with status 401.
func (e *HTTPResponse) Unauthorized(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 401
e.Error = "unauthorized"
e.Message = message
e.Path = path
return e
}
// Forbidden returns a new HTTPResponse with status 403.
func (e *HTTPResponse) Forbidden(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 403
e.Error = "forbidden"
e.Message = message
e.Path = path
return e
}
// NotFound returns a new HTTPResponse with status 404.
func (e *HTTPResponse) NotFound(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 404
e.Error = "not found"
e.Message = message
e.Path = path
return e
}
// Conflict returns a new HTTPResponse with status 409.
func (e *HTTPResponse) Conflict(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 409
e.Error = "conflict"
e.Message = message
e.Path = path
return e
}
// InternalServerError returns a new HTTPResponse with status 500.
func (e *HTTPResponse) InternalServerError(message, path string) *HTTPResponse {
e.Timestamp = time.Now().Unix()
e.Status = 500
e.Error = "internal server error"
e.Message = message
e.Path = path
return e
}
// ToJson returns the HTTPResponse as a JSON string.
func (e *HTTPResponse) ToJson() string {
// FormatInt is 1.3 times faster than Itoa
// FormatInt + int64() is 1.1 times faster than Itoa
return `{
"timestamp": ` + strconv.FormatInt(e.Timestamp, 10) + `,
"status": ` + strconv.FormatInt(int64(e.Status), 10) + `,
"error": "` + e.Error + `",
"message": "` + e.Message + `",
"path": "` + e.Path + `"
}`
}