From 065673fae2bef37f731a7930ca1d7cdd79d6463c Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 21 Jun 2026 22:15:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=97=A5=E5=BF=97=EF=BC=88=E5=88=9B=E5=BB=BA?= =?UTF-8?q?/=E9=94=80=E6=AF=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/api/session.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/internal/api/session.go b/backend/internal/api/session.go index 78bae2d..9690258 100644 --- a/backend/internal/api/session.go +++ b/backend/internal/api/session.go @@ -8,6 +8,7 @@ import ( "github.com/hhs/camtalk/internal/models" "github.com/hhs/camtalk/internal/session" + "github.com/hhs/camtalk/internal/trace" ) // SessionHandler 提供会话相关的 REST 端点。 @@ -27,6 +28,8 @@ type CreateSessionRequest struct { // CreateSession POST /api/sessions — 创建新会话。 func (h *SessionHandler) CreateSession(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) + var req CreateSessionRequest // 请求体可选,解析失败不报错(使用默认配置) _ = c.ShouldBindJSON(&req) @@ -38,6 +41,8 @@ func (h *SessionHandler) CreateSession(c *gin.Context) { sessionID, err := h.sessionMgr.Create(c.Request.Context(), "", cfg) if err != nil { + log.Errorw("create session failed", + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": "INTERNAL_ERROR", "message": "failed to create session", @@ -48,6 +53,9 @@ func (h *SessionHandler) CreateSession(c *gin.Context) { // 获取创建后的会话以返回 created_at sess, err := h.sessionMgr.Get(c.Request.Context(), sessionID) if err != nil { + log.Errorw("retrieve created session failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": "INTERNAL_ERROR", "message": "failed to retrieve created session", @@ -55,6 +63,8 @@ func (h *SessionHandler) CreateSession(c *gin.Context) { return } + log.Infow("session created", + "session_id", sess.ID) c.JSON(http.StatusCreated, gin.H{ "session_id": sess.ID, "created_at": sess.CreatedAt, @@ -63,6 +73,7 @@ func (h *SessionHandler) CreateSession(c *gin.Context) { // DestroySession DELETE /api/sessions/:id — 销毁会话。 func (h *SessionHandler) DestroySession(c *gin.Context) { + log := trace.FromContext(c.Request.Context()) sessionID := c.Param("id") err := h.sessionMgr.Destroy(c.Request.Context(), sessionID) @@ -74,6 +85,9 @@ func (h *SessionHandler) DestroySession(c *gin.Context) { }) return } + log.Errorw("destroy session failed", + "session_id", sessionID, + "error", err) c.JSON(http.StatusInternalServerError, gin.H{ "code": "INTERNAL_ERROR", "message": "failed to destroy session", @@ -81,6 +95,8 @@ func (h *SessionHandler) DestroySession(c *gin.Context) { return } + log.Infow("session destroyed", + "session_id", sessionID) c.Status(http.StatusNoContent) }