mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-10 11:17:41 +08:00
refactor backend API structure
This commit is contained in:
16
internal/httpapi/admin/devcapture/deps.go
Normal file
16
internal/httpapi/admin/devcapture/deps.go
Normal 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
|
||||
26
internal/httpapi/admin/devcapture/handler_dev_capture.go
Normal file
26
internal/httpapi/admin/devcapture/handler_dev_capture.go
Normal 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",
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
8
internal/httpapi/admin/devcapture/routes.go
Normal file
8
internal/httpapi/admin/devcapture/routes.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user