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 71f4222..c98ac14 100644 --- a/trading_assistant_api/services/user/internal/handler/user_handler.go +++ b/trading_assistant_api/services/user/internal/handler/user_handler.go @@ -6,6 +6,7 @@ import ( "common/httpx" "common/codes" + "common/logger" "user/internal/model" "user/internal/service" ) @@ -20,74 +21,92 @@ func New(s *service.Service) *Handler { func (h *Handler) Register(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path) + httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed)) return } var req model.RegisterReq if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - httpx.BadRequest(w, "invalid json") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid json path=%s", r.URL.Path) + httpx.BadRequest(w, r, "invalid json") return } userID, token, err := h.S.Register(req.Account, req.Password) if err != nil { switch err { case service.ErrInvalidInput: - httpx.BadRequest(w, "invalid account or password") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid input account=%s", req.Account) + httpx.BadRequest(w, r, "invalid account or password") case service.ErrAccountExists: - httpx.Conflict(w, "account exists") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("account exists account=%s", req.Account) + httpx.Conflict(w, r, "account exists") default: - httpx.InternalError(w) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("internal error account=%s", req.Account) + httpx.InternalError(w, r) } return } - httpx.Created(w, map[string]string{"user_id": userID, "token": token}) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("register success user=%s", userID) + httpx.Created(w, r, map[string]string{"user_id": userID, "token": token}) } func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path) + httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed)) return } var req model.LoginReq if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - httpx.BadRequest(w, "invalid json") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid json path=%s", r.URL.Path) + httpx.BadRequest(w, r, "invalid json") return } userID, token, err := h.S.Login(req.Account, req.Password) if err != nil { switch err { case service.ErrInvalidInput: - httpx.BadRequest(w, "invalid account or password") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("invalid input account=%s", req.Account) + httpx.BadRequest(w, r, "invalid account or password") case service.ErrUnauthorized: - httpx.Unauthorized(w, "unauthorized") + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("unauthorized account=%s", req.Account) + httpx.Unauthorized(w, r, "unauthorized") default: - httpx.InternalError(w) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("internal error account=%s", req.Account) + httpx.InternalError(w, r) } return } - httpx.OK(w, map[string]string{"user_id": userID, "token": token}) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("login success user=%s", userID) + httpx.OK(w, r, map[string]string{"user_id": userID, "token": token}) } func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path) + httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed)) return } - httpx.OK(w, nil) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("healthz ok") + httpx.OK(w, r, nil) } func (h *Handler) Version(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path) + httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed)) return } - httpx.OK(w, map[string]string{"version": "user-service v0.1.0"}) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("version ok") + httpx.OK(w, r, map[string]string{"version": "user-service v0.1.0"}) } func (h *Handler) Root(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed)) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("method not allowed path=%s", r.URL.Path) + httpx.MethodNotAllowed(w, r, string(codes.MethodNotAllowed)) return } - httpx.OK(w, map[string]string{"service": "user", "user_id": httpx.UserID(r)}) + logger.WithPrefix("rid="+httpx.RequestIDFromContext(r)).Printf("root ok user=%s", httpx.UserID(r)) + httpx.OK(w, r, map[string]string{"service": "user", "user_id": httpx.UserID(r)}) }