feat: expose backend debug log endpoints

This commit is contained in:
2026-04-02 23:51:44 +08:00
parent 9ec25b94f1
commit 98839e9782
5 changed files with 36 additions and 2 deletions

View File

@@ -1,9 +1,12 @@
package handler
import (
"encoding/json"
"fmt"
"net/http"
"time"
"ckwk/internal/dto"
"ckwk/pkg/log"
"github.com/gin-gonic/gin"
@@ -16,6 +19,26 @@ var debugLogUpgrader = websocket.Upgrader{
},
}
func DebugLogs(ctx *gin.Context) {
ctx.JSON(http.StatusOK, dto.Success(map[string]any{
"list": log.Entries(),
}))
}
func DebugLogsDownload(ctx *gin.Context) {
entries := log.Entries()
content, err := json.MarshalIndent(entries, "", " ")
if err != nil {
ctx.JSON(http.StatusInternalServerError, dto.Error(500, "日志导出失败"))
return
}
filename := fmt.Sprintf("wk-debug-logs-%s.json", time.Now().Format("20060102-150405"))
ctx.Header("Content-Type", "application/json; charset=utf-8")
ctx.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
ctx.Data(http.StatusOK, "application/json; charset=utf-8", content)
}
func DebugLogWS(ctx *gin.Context) {
conn, err := debugLogUpgrader.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {