From ec07824a4d24df2ca23119310a963b8c31b5f97c Mon Sep 17 00:00:00 2001 From: vipg Date: Mon, 9 Feb 2026 16:59:04 +0800 Subject: [PATCH] add --- trading_assistant_api/common/codes/codes.go | 12 ++++++ trading_assistant_api/common/httpx/httpx.go | 43 +++++++++++++++++++ .../user/internal/handler/user_handler.go | 39 ++++++++--------- 3 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 trading_assistant_api/common/codes/codes.go create mode 100644 trading_assistant_api/common/httpx/httpx.go diff --git a/trading_assistant_api/common/codes/codes.go b/trading_assistant_api/common/codes/codes.go new file mode 100644 index 0000000..b30b181 --- /dev/null +++ b/trading_assistant_api/common/codes/codes.go @@ -0,0 +1,12 @@ +package codes + +type Code string + +const ( + OK Code = "ok" + InvalidInput Code = "invalid_input" + Unauthorized Code = "unauthorized" + Conflict Code = "conflict" + InternalError Code = "internal_error" + MethodNotAllowed Code = "method_not_allowed" +) diff --git a/trading_assistant_api/common/httpx/httpx.go b/trading_assistant_api/common/httpx/httpx.go new file mode 100644 index 0000000..915608a --- /dev/null +++ b/trading_assistant_api/common/httpx/httpx.go @@ -0,0 +1,43 @@ +package httpx + +import ( + "encoding/json" + "net/http" + + "common/types" + "common/codes" +) + +func WriteJSON(w http.ResponseWriter, status int, ok bool, 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}) +} + +func OK(w http.ResponseWriter, data interface{}) { + WriteJSON(w, http.StatusOK, true, string(codes.OK), data) +} + +func Created(w http.ResponseWriter, data interface{}) { + WriteJSON(w, http.StatusCreated, true, string(codes.OK), data) +} + +func BadRequest(w http.ResponseWriter, msg string) { + WriteJSON(w, http.StatusBadRequest, false, msg, map[string]string{"code": string(codes.InvalidInput)}) +} + +func Unauthorized(w http.ResponseWriter, msg string) { + WriteJSON(w, http.StatusUnauthorized, false, msg, map[string]string{"code": string(codes.Unauthorized)}) +} + +func Conflict(w http.ResponseWriter, msg string) { + WriteJSON(w, http.StatusConflict, false, msg, map[string]string{"code": string(codes.Conflict)}) +} + +func MethodNotAllowed(w http.ResponseWriter, msg string) { + WriteJSON(w, http.StatusMethodNotAllowed, false, msg, map[string]string{"code": string(codes.MethodNotAllowed)}) +} + +func InternalError(w http.ResponseWriter) { + WriteJSON(w, http.StatusInternalServerError, false, string(codes.InternalError), map[string]string{"code": string(codes.InternalError)}) +} diff --git a/trading_assistant_api/services/user/internal/handler/user_handler.go b/trading_assistant_api/services/user/internal/handler/user_handler.go index 597155c..7291505 100644 --- a/trading_assistant_api/services/user/internal/handler/user_handler.go +++ b/trading_assistant_api/services/user/internal/handler/user_handler.go @@ -4,7 +4,8 @@ import ( "encoding/json" "net/http" - "common/types" + "common/httpx" + "common/codes" "user/internal/model" "user/internal/service" ) @@ -19,68 +20,62 @@ func New(s *service.Service) *Handler { func (h *Handler) Register(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil) + httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) return } var req model.RegisterReq if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - writeJSON(w, http.StatusBadRequest, false, "invalid json", nil) + httpx.BadRequest(w, "invalid json") return } userID, err := h.S.Register(req.Account, req.Password) if err != nil { switch err { case service.ErrInvalidInput: - writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil) + httpx.BadRequest(w, "invalid account or password") case service.ErrAccountExists: - writeJSON(w, http.StatusConflict, false, "account exists", nil) + httpx.Conflict(w, "account exists") default: - writeJSON(w, http.StatusInternalServerError, false, "internal error", nil) + httpx.InternalError(w) } return } - writeJSON(w, http.StatusCreated, true, "ok", map[string]string{"user_id": userID}) + httpx.Created(w, map[string]string{"user_id": userID}) } func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil) + httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) return } var req model.LoginReq if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - writeJSON(w, http.StatusBadRequest, false, "invalid json", nil) + httpx.BadRequest(w, "invalid json") return } userID, err := h.S.Login(req.Account, req.Password) if err != nil { switch err { case service.ErrInvalidInput: - writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil) + httpx.BadRequest(w, "invalid account or password") case service.ErrUnauthorized: - writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil) + httpx.Unauthorized(w, "unauthorized") default: - writeJSON(w, http.StatusInternalServerError, false, "internal error", nil) + httpx.InternalError(w) } return } - writeJSON(w, http.StatusOK, true, "ok", map[string]string{"user_id": userID}) + httpx.OK(w, map[string]string{"user_id": userID}) } func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) { - writeJSON(w, http.StatusOK, true, "ok", nil) + httpx.OK(w, nil) } func (h *Handler) Version(w http.ResponseWriter, r *http.Request) { - writeJSON(w, http.StatusOK, true, "ok", map[string]string{"version": "user-service v0.1.0"}) + httpx.OK(w, map[string]string{"version": "user-service v0.1.0"}) } func (h *Handler) Root(w http.ResponseWriter, r *http.Request) { - writeJSON(w, http.StatusOK, true, "ok", map[string]string{"service": "user"}) -} - -func writeJSON(w http.ResponseWriter, code int, status bool, msg string, data interface{}) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(code) - json.NewEncoder(w).Encode(types.Response{Status: status, Message: msg, Data: data}) + httpx.OK(w, map[string]string{"service": "user"}) }