add
This commit is contained in:
@@ -8,48 +8,36 @@ import (
|
|||||||
"common/codes"
|
"common/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WriteJSON(w http.ResponseWriter, status int, ok bool, code int, msg string, data interface{}) {
|
func WriteJSON(w http.ResponseWriter, r *http.Request, status int, ok bool, code int, msg string, data interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
json.NewEncoder(w).Encode(types.Response{Status: ok, Message: msg, Code: code, Data: data})
|
json.NewEncoder(w).Encode(types.Response{Status: ok, Message: msg, Code: code, RequestID: RequestIDFromContext(r), Data: data})
|
||||||
}
|
}
|
||||||
|
|
||||||
func OK(w http.ResponseWriter, r *http.Request, data interface{}) {
|
func OK(w http.ResponseWriter, r *http.Request, data interface{}) {
|
||||||
WriteJSON(w, http.StatusOK, true, codes.ValueOK, string(codes.OK), addRequestID(r, data))
|
WriteJSON(w, r, http.StatusOK, true, codes.ValueOK, string(codes.OK), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Created(w http.ResponseWriter, r *http.Request, data interface{}) {
|
func Created(w http.ResponseWriter, r *http.Request, data interface{}) {
|
||||||
WriteJSON(w, http.StatusCreated, true, codes.ValueOK, string(codes.OK), addRequestID(r, data))
|
WriteJSON(w, r, http.StatusCreated, true, codes.ValueOK, string(codes.OK), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BadRequest(w http.ResponseWriter, r *http.Request, msg string) {
|
func BadRequest(w http.ResponseWriter, r *http.Request, msg string) {
|
||||||
WriteJSON(w, http.StatusBadRequest, false, codes.ValueInvalidInput, msg, addRequestID(r, nil))
|
WriteJSON(w, r, http.StatusBadRequest, false, codes.ValueInvalidInput, msg, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unauthorized(w http.ResponseWriter, r *http.Request, msg string) {
|
func Unauthorized(w http.ResponseWriter, r *http.Request, msg string) {
|
||||||
WriteJSON(w, http.StatusUnauthorized, false, codes.ValueUnauthorized, msg, addRequestID(r, nil))
|
WriteJSON(w, r, http.StatusUnauthorized, false, codes.ValueUnauthorized, msg, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Conflict(w http.ResponseWriter, r *http.Request, msg string) {
|
func Conflict(w http.ResponseWriter, r *http.Request, msg string) {
|
||||||
WriteJSON(w, http.StatusConflict, false, codes.ValueConflict, msg, addRequestID(r, nil))
|
WriteJSON(w, r, http.StatusConflict, false, codes.ValueConflict, msg, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MethodNotAllowed(w http.ResponseWriter, r *http.Request, msg string) {
|
func MethodNotAllowed(w http.ResponseWriter, r *http.Request, msg string) {
|
||||||
WriteJSON(w, http.StatusMethodNotAllowed, false, codes.ValueMethodNotAllowed, msg, addRequestID(r, nil))
|
WriteJSON(w, r, http.StatusMethodNotAllowed, false, codes.ValueMethodNotAllowed, msg, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InternalError(w http.ResponseWriter, r *http.Request) {
|
func InternalError(w http.ResponseWriter, r *http.Request) {
|
||||||
WriteJSON(w, http.StatusInternalServerError, false, codes.ValueInternalError, string(codes.InternalError), addRequestID(r, nil))
|
WriteJSON(w, r, http.StatusInternalServerError, false, codes.ValueInternalError, string(codes.InternalError), nil)
|
||||||
}
|
|
||||||
|
|
||||||
func addRequestID(r *http.Request, data interface{}) interface{} {
|
|
||||||
rid := RequestIDFromContext(r)
|
|
||||||
if m, ok := data.(map[string]string); ok {
|
|
||||||
m["request_id"] = rid
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
if data == nil {
|
|
||||||
return map[string]string{"request_id": rid}
|
|
||||||
}
|
|
||||||
return map[string]interface{}{"request_id": rid, "data": data}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,6 @@ type Response struct {
|
|||||||
Status bool `json:"status"`
|
Status bool `json:"status"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
|
RequestID string `json:"request_id"`
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user