From 2b2fd162a2e23be456f10e40ff064dd4fd885edd Mon Sep 17 00:00:00 2001 From: vipg Date: Tue, 10 Feb 2026 11:20:57 +0800 Subject: [PATCH] add --- trading_assistant_api/common/httpx/httpx.go | 30 ++++++------------- .../common/types/response.go | 1 + 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/trading_assistant_api/common/httpx/httpx.go b/trading_assistant_api/common/httpx/httpx.go index 1b165f4..fa59ba1 100644 --- a/trading_assistant_api/common/httpx/httpx.go +++ b/trading_assistant_api/common/httpx/httpx.go @@ -8,48 +8,36 @@ import ( "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.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{}) { - 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{}) { - 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) { - 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) { - 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) { - 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) { - 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) { - WriteJSON(w, http.StatusInternalServerError, false, codes.ValueInternalError, string(codes.InternalError), addRequestID(r, 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} + WriteJSON(w, r, http.StatusInternalServerError, false, codes.ValueInternalError, string(codes.InternalError), nil) } diff --git a/trading_assistant_api/common/types/response.go b/trading_assistant_api/common/types/response.go index aedea7b..116b5cf 100644 --- a/trading_assistant_api/common/types/response.go +++ b/trading_assistant_api/common/types/response.go @@ -4,5 +4,6 @@ type Response struct { Status bool `json:"status"` Message string `json:"message"` Code int `json:"code"` + RequestID string `json:"request_id"` Data interface{} `json:"data,omitempty"` }