-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
48 lines (42 loc) · 1.44 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
package khota_handler
import (
"encoding/json"
"net/http"
klog "k8s.io/klog/v2"
)
type Response struct {
Msg CustomMsg
Status string
StatusCode int
Err error
}
func NewResponse(msg CustomMsg, status string, err error, statusCode int) *Response {
return &Response{Msg: msg, Status: status, StatusCode: statusCode, Err: err}
}
func (res *Response) LogResponse() {
if res.Err != nil {
klog.Errorf("ErrMsg: [%s], Status: [%s], Error: [%s], statusCode [%d]", res.Msg, res.Status, res.Err.Error(), res.StatusCode)
} else {
klog.Infof("Msg: [%s], Status: [%s], Error: [%s], statusCode [%d]", res.Msg, res.Status, "", res.StatusCode)
}
}
func (res *Response) SetResponse(w *http.ResponseWriter) {
(*w).Header().Set("Content-Type", "application/json; charset=UTF-8")
(*w).WriteHeader(res.StatusCode)
json.NewEncoder(*w).Encode(res)
}
func (res *Response) SetMsgResponse(w *http.ResponseWriter) {
jsonData, err := json.Marshal(res.Msg)
if err != nil {
klog.Errorf("ErrMsg: Unable to parse response Message json [%s], Error: [%s]", res.Msg, err.Error())
res.StatusCode = http.StatusInternalServerError
}
(*w).Header().Set("Content-Type", "application/json; charset=UTF-8")
(*w).WriteHeader(res.StatusCode)
(*w).Write(jsonData)
}
func sendJsonResponse(jsonByte []byte, statusCode int, w *http.ResponseWriter) {
(*w).Header().Set("Content-Type", "application/json; charset=UTF-8")
(*w).WriteHeader(statusCode)
(*w).Write(jsonByte)
}