add
This commit is contained in:
10
trading_assistant_api/common/codes/values.go
Normal file
10
trading_assistant_api/common/codes/values.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package codes
|
||||||
|
|
||||||
|
const (
|
||||||
|
ValueOK = 0
|
||||||
|
ValueInvalidInput = 1001
|
||||||
|
ValueUnauthorized = 1002
|
||||||
|
ValueConflict = 1003
|
||||||
|
ValueMethodNotAllowed = 1004
|
||||||
|
ValueInternalError = 1500
|
||||||
|
)
|
||||||
@@ -8,38 +8,38 @@ import (
|
|||||||
"common/codes"
|
"common/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WriteJSON(w http.ResponseWriter, status int, ok bool, msg string, data interface{}) {
|
func WriteJSON(w http.ResponseWriter, 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, Data: data})
|
json.NewEncoder(w).Encode(types.Response{Status: ok, Message: msg, Code: code, 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, string(codes.OK), addRequestID(r, data))
|
WriteJSON(w, http.StatusOK, true, codes.ValueOK, string(codes.OK), addRequestID(r, 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, string(codes.OK), addRequestID(r, data))
|
WriteJSON(w, http.StatusCreated, true, codes.ValueOK, string(codes.OK), addRequestID(r, 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, msg, addRequestID(r, map[string]string{"code": string(codes.InvalidInput)}))
|
WriteJSON(w, http.StatusBadRequest, false, codes.ValueInvalidInput, msg, addRequestID(r, 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, msg, addRequestID(r, map[string]string{"code": string(codes.Unauthorized)}))
|
WriteJSON(w, http.StatusUnauthorized, false, codes.ValueUnauthorized, msg, addRequestID(r, 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, msg, addRequestID(r, map[string]string{"code": string(codes.Conflict)}))
|
WriteJSON(w, http.StatusConflict, false, codes.ValueConflict, msg, addRequestID(r, 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, msg, addRequestID(r, map[string]string{"code": string(codes.MethodNotAllowed)}))
|
WriteJSON(w, http.StatusMethodNotAllowed, false, codes.ValueMethodNotAllowed, msg, addRequestID(r, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func InternalError(w http.ResponseWriter, r *http.Request) {
|
func InternalError(w http.ResponseWriter, r *http.Request) {
|
||||||
WriteJSON(w, http.StatusInternalServerError, false, string(codes.InternalError), addRequestID(r, map[string]string{"code": string(codes.InternalError)}))
|
WriteJSON(w, http.StatusInternalServerError, false, codes.ValueInternalError, string(codes.InternalError), addRequestID(r, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRequestID(r *http.Request, data interface{}) interface{} {
|
func addRequestID(r *http.Request, data interface{}) interface{} {
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ package types
|
|||||||
type Response struct {
|
type Response struct {
|
||||||
Status bool `json:"status"`
|
Status bool `json:"status"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
|
Code int `json:"code"`
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user