-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
102 lines (85 loc) · 3.02 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
package u_handler
import (
"encoding/json"
"fmt"
"net/http"
"github.com/getnimbus/ultrago/u_logger"
"github.com/sirupsen/logrus"
)
func (h *BaseHandler) BadRequest(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusBadRequest)
h.errorHandler(w, r, http.StatusBadRequest, err)
}
func (h *BaseHandler) Unauthorized(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusUnauthorized)
h.errorHandler(w, r, http.StatusUnauthorized, err)
}
func (h *BaseHandler) Forbidden(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusForbidden)
h.errorHandler(w, r, http.StatusForbidden, err)
}
func (h *BaseHandler) NotFound(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusNotFound)
h.errorHandler(w, r, http.StatusNotFound, err)
}
func (h *BaseHandler) TooManyRequests(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusTooManyRequests)
h.errorHandler(w, r, http.StatusTooManyRequests, err)
}
func (h *BaseHandler) Internal(w http.ResponseWriter, r *http.Request, err error) {
h.logging(r, http.StatusInternalServerError)
h.errorHandler(w, r, http.StatusInternalServerError, err)
}
func (h *BaseHandler) Success(w http.ResponseWriter, r *http.Request, data interface{}) {
h.processor.BeforeSuccess(w, r, data)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": nil,
"data": data,
"success": true,
})
}
func (h *BaseHandler) FileSuccess(w http.ResponseWriter, r *http.Request, data interface{}, fileName string) {
h.processor.BeforeSuccess(w, r, data)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
w.Header().Set("Content-Type", "application/octet-stream")
w.WriteHeader(http.StatusOK)
byteData, _ := json.Marshal(data)
w.Write(byteData)
}
func (h *BaseHandler) PaginateSuccess(w http.ResponseWriter, r *http.Request, data interface{}, total int64) {
h.processor.BeforeSuccess(w, r, data)
offset := h.RequestParamInt(r, OffsetKey, 0)
limit := h.RequestParamInt(r, LimitKey, 10)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": nil,
"data": data,
"success": true,
"pagination": map[string]interface{}{
"total": total,
"offset": offset,
"limit": limit,
},
})
}
func (h *BaseHandler) logging(r *http.Request, statusCode int) {
_, ok := h.logConfig[statusCode]
if ok {
_, logger := u_logger.GetLogger(r.Context())
logger.WithFields(logrus.Fields{"url": r.URL.Path}).
Errorf("got code=%d", statusCode)
}
}
func (h *BaseHandler) errorHandler(w http.ResponseWriter, r *http.Request, statusCode int, err error) {
h.processor.BeforeError(w, r, err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": h.processor.FormatErr(w, r, err),
"data": nil,
"success": false,
})
}