From 32584e25b24b34c97695fbf5d0d4918ec5e2adca Mon Sep 17 00:00:00 2001 From: vipg Date: Mon, 9 Feb 2026 17:59:34 +0800 Subject: [PATCH] add --- trading_assistant_api/common/codes/values.go | 10 ++++++++++ trading_assistant_api/common/httpx/httpx.go | 18 +++++++++--------- trading_assistant_api/common/types/response.go | 1 + 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 trading_assistant_api/common/codes/values.go diff --git a/trading_assistant_api/common/codes/values.go b/trading_assistant_api/common/codes/values.go new file mode 100644 index 0000000..df8d126 --- /dev/null +++ b/trading_assistant_api/common/codes/values.go @@ -0,0 +1,10 @@ +package codes + +const ( + ValueOK = 0 + ValueInvalidInput = 1001 + ValueUnauthorized = 1002 + ValueConflict = 1003 + ValueMethodNotAllowed = 1004 + ValueInternalError = 1500 +) diff --git a/trading_assistant_api/common/httpx/httpx.go b/trading_assistant_api/common/httpx/httpx.go index 3bb1357..1b165f4 100644 --- a/trading_assistant_api/common/httpx/httpx.go +++ b/trading_assistant_api/common/httpx/httpx.go @@ -8,38 +8,38 @@ import ( "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.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{}) { - 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{}) { - 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) { - 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) { - 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) { - 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) { - 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) { - 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{} { diff --git a/trading_assistant_api/common/types/response.go b/trading_assistant_api/common/types/response.go index c5e5f0e..aedea7b 100644 --- a/trading_assistant_api/common/types/response.go +++ b/trading_assistant_api/common/types/response.go @@ -3,5 +3,6 @@ package types type Response struct { Status bool `json:"status"` Message string `json:"message"` + Code int `json:"code"` Data interface{} `json:"data,omitempty"` }