refactor backend API structure

This commit is contained in:
CJACK
2026-04-26 06:58:20 +08:00
parent 8a91fef6ab
commit abc96a37d8
207 changed files with 2675 additions and 1344 deletions

View File

@@ -0,0 +1,16 @@
package devcapture
import (
"ds2api/internal/chathistory"
adminshared "ds2api/internal/httpapi/admin/shared"
)
type Handler struct {
Store adminshared.ConfigStore
Pool adminshared.PoolController
DS adminshared.DeepSeekCaller
OpenAI adminshared.OpenAIChatCaller
ChatHistory *chathistory.Store
}
var writeJSON = adminshared.WriteJSON

View File

@@ -0,0 +1,26 @@
package devcapture
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 devcapture
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)
}
}

View File

@@ -0,0 +1,8 @@
package devcapture
import "github.com/go-chi/chi/v5"
func RegisterRoutes(r chi.Router, h *Handler) {
r.Get("/dev/captures", h.getDevCaptures)
r.Delete("/dev/captures", h.clearDevCaptures)
}