feat: implement local dev packet capture functionality with admin endpoints and configurable limits for debugging.

This commit is contained in:
CJACK
2026-02-20 03:46:15 +08:00
parent dec9d03fc5
commit 541816f2ab
8 changed files with 449 additions and 0 deletions

View File

@@ -36,5 +36,7 @@ func RegisterRoutes(r chi.Router, h *Handler) {
pr.Post("/vercel/sync", h.syncVercel)
pr.Get("/vercel/status", h.vercelStatus)
pr.Get("/export", h.exportConfig)
pr.Get("/dev/captures", h.getDevCaptures)
pr.Delete("/dev/captures", h.clearDevCaptures)
})
}

View File

@@ -0,0 +1,26 @@
package admin
import (
"net/http"
"ds2api/internal/devcapture"
)
func (h *Handler) getDevCaptures(w http.ResponseWriter, _ *http.Request) {
store := devcapture.Global()
writeJSON(w, http.StatusOK, map[string]any{
"enabled": store.Enabled(),
"limit": store.Limit(),
"max_body_bytes": store.MaxBodyBytes(),
"items": store.Snapshot(),
})
}
func (h *Handler) clearDevCaptures(w http.ResponseWriter, _ *http.Request) {
store := devcapture.Global()
store.Clear()
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"detail": "capture logs cleared",
})
}

View File

@@ -0,0 +1,45 @@
package admin
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetDevCapturesShape(t *testing.T) {
h := &Handler{}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/admin/dev/captures", nil)
h.getDevCaptures(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String())
}
var out map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("decode failed: %v", err)
}
if _, ok := out["enabled"]; !ok {
t.Fatalf("expected enabled field, got %#v", out)
}
if _, ok := out["items"]; !ok {
t.Fatalf("expected items field, got %#v", out)
}
}
func TestClearDevCapturesShape(t *testing.T) {
h := &Handler{}
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodDelete, "/admin/dev/captures", nil)
h.clearDevCaptures(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String())
}
var out map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("decode failed: %v", err)
}
if out["success"] != true {
t.Fatalf("expected success=true, got %#v", out)
}
}