This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
response.go
109 lines (95 loc) · 2.76 KB
/
response.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
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"github.com/google/uuid"
"github.com/mailru/easyjson"
"github.com/masseelch/elk/internal/uuid/ent"
)
// Basic HTTP Error Response
type ErrResponse struct {
Code int `json:"code"` // http response status code
Status string `json:"status"` // user-level status message
Errors interface{} `json:"errors,omitempty"` // application-level error
}
func (e ErrResponse) MarshalToHTTPResponseWriter(w http.ResponseWriter) (int, error) {
d, err := easyjson.Marshal(e)
if err != nil {
return 0, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(e.Code)
return w.Write(d)
}
func BadRequest(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusBadRequest,
Status: http.StatusText(http.StatusBadRequest),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Conflict(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusConflict,
Status: http.StatusText(http.StatusConflict),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Forbidden(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusForbidden,
Status: http.StatusText(http.StatusForbidden),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func InternalServerError(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusInternalServerError,
Status: http.StatusText(http.StatusInternalServerError),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func NotFound(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusNotFound,
Status: http.StatusText(http.StatusNotFound),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Unauthorized(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusUnauthorized,
Status: http.StatusText(http.StatusUnauthorized),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
type (
// User3451555716View represents the data serialized for the following serialization group combinations:
// []
User3451555716View struct {
ID int `json:"id,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
}
User3451555716Views []*User3451555716View
)
func NewUser3451555716View(e *ent.User) *User3451555716View {
if e == nil {
return nil
}
return &User3451555716View{
ID: e.ID,
UUID: e.UUID,
}
}
func NewUser3451555716Views(es []*ent.User) User3451555716Views {
if len(es) == 0 {
return nil
}
r := make(User3451555716Views, len(es))
for i, e := range es {
r[i] = NewUser3451555716View(e)
}
return r
}