feat: 添加会话接口日志(创建/销毁)
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hhs/camtalk/internal/models"
|
"github.com/hhs/camtalk/internal/models"
|
||||||
"github.com/hhs/camtalk/internal/session"
|
"github.com/hhs/camtalk/internal/session"
|
||||||
|
"github.com/hhs/camtalk/internal/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SessionHandler 提供会话相关的 REST 端点。
|
// SessionHandler 提供会话相关的 REST 端点。
|
||||||
@@ -27,6 +28,8 @@ type CreateSessionRequest struct {
|
|||||||
|
|
||||||
// CreateSession POST /api/sessions — 创建新会话。
|
// CreateSession POST /api/sessions — 创建新会话。
|
||||||
func (h *SessionHandler) CreateSession(c *gin.Context) {
|
func (h *SessionHandler) CreateSession(c *gin.Context) {
|
||||||
|
log := trace.FromContext(c.Request.Context())
|
||||||
|
|
||||||
var req CreateSessionRequest
|
var req CreateSessionRequest
|
||||||
// 请求体可选,解析失败不报错(使用默认配置)
|
// 请求体可选,解析失败不报错(使用默认配置)
|
||||||
_ = c.ShouldBindJSON(&req)
|
_ = c.ShouldBindJSON(&req)
|
||||||
@@ -38,6 +41,8 @@ func (h *SessionHandler) CreateSession(c *gin.Context) {
|
|||||||
|
|
||||||
sessionID, err := h.sessionMgr.Create(c.Request.Context(), "", cfg)
|
sessionID, err := h.sessionMgr.Create(c.Request.Context(), "", cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorw("create session failed",
|
||||||
|
"error", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"code": "INTERNAL_ERROR",
|
"code": "INTERNAL_ERROR",
|
||||||
"message": "failed to create session",
|
"message": "failed to create session",
|
||||||
@@ -48,6 +53,9 @@ func (h *SessionHandler) CreateSession(c *gin.Context) {
|
|||||||
// 获取创建后的会话以返回 created_at
|
// 获取创建后的会话以返回 created_at
|
||||||
sess, err := h.sessionMgr.Get(c.Request.Context(), sessionID)
|
sess, err := h.sessionMgr.Get(c.Request.Context(), sessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorw("retrieve created session failed",
|
||||||
|
"session_id", sessionID,
|
||||||
|
"error", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"code": "INTERNAL_ERROR",
|
"code": "INTERNAL_ERROR",
|
||||||
"message": "failed to retrieve created session",
|
"message": "failed to retrieve created session",
|
||||||
@@ -55,6 +63,8 @@ func (h *SessionHandler) CreateSession(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infow("session created",
|
||||||
|
"session_id", sess.ID)
|
||||||
c.JSON(http.StatusCreated, gin.H{
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
"session_id": sess.ID,
|
"session_id": sess.ID,
|
||||||
"created_at": sess.CreatedAt,
|
"created_at": sess.CreatedAt,
|
||||||
@@ -63,6 +73,7 @@ func (h *SessionHandler) CreateSession(c *gin.Context) {
|
|||||||
|
|
||||||
// DestroySession DELETE /api/sessions/:id — 销毁会话。
|
// DestroySession DELETE /api/sessions/:id — 销毁会话。
|
||||||
func (h *SessionHandler) DestroySession(c *gin.Context) {
|
func (h *SessionHandler) DestroySession(c *gin.Context) {
|
||||||
|
log := trace.FromContext(c.Request.Context())
|
||||||
sessionID := c.Param("id")
|
sessionID := c.Param("id")
|
||||||
|
|
||||||
err := h.sessionMgr.Destroy(c.Request.Context(), sessionID)
|
err := h.sessionMgr.Destroy(c.Request.Context(), sessionID)
|
||||||
@@ -74,6 +85,9 @@ func (h *SessionHandler) DestroySession(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Errorw("destroy session failed",
|
||||||
|
"session_id", sessionID,
|
||||||
|
"error", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"code": "INTERNAL_ERROR",
|
"code": "INTERNAL_ERROR",
|
||||||
"message": "failed to destroy session",
|
"message": "failed to destroy session",
|
||||||
@@ -81,6 +95,8 @@ func (h *SessionHandler) DestroySession(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infow("session destroyed",
|
||||||
|
"session_id", sessionID)
|
||||||
c.Status(http.StatusNoContent)
|
c.Status(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user