Files
xk-hy-transit-go/internal/logweb/supervise_handlers.go
2026-06-05 16:36:39 +08:00

69 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logweb
import (
"encoding/json"
"net/http"
"strings"
syncer "xk-hy-transit-go/internal/sync"
"xk-hy-transit-go/internal/syncgate"
)
func (s *Server) handleSuperviseStepPage(w http.ResponseWriter, r *http.Request) {
s.handlePage("supervise_step.html")(w, r)
}
func (s *Server) handleAPISuperviseStepTest(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeErr(w, 405, "POST only")
return
}
sr := asSyncRunner(s.deps.Runner)
if sr == nil {
writeErr(w, 503, "未配置 Runner请使用 transit serve")
return
}
var body struct {
Step string `json:"step"`
Date string `json:"date"`
Limit *int `json:"limit"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, 400, "invalid json")
return
}
step := strings.TrimSpace(body.Step)
if step == "" {
writeErr(w, 400, "step required (recipe|verification)")
return
}
limit := 1
if body.Limit != nil {
limit = *body.Limit
}
anchor := syncer.ResolveAnchorDate(s.deps.Cfg, strings.TrimSpace(body.Date))
var result *syncer.StepTestResult
var runErr error
if !syncgate.TryRun(step, anchor, func() error {
result, runErr = sr.TestSuperviseStep(step, anchor, limit)
return runErr
}) {
writeErr(w, 409, "已有同步任务在执行中")
return
}
if runErr != nil {
payload := map[string]any{"error": runErr.Error()}
if result != nil {
payload["result"] = result
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(502)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
_ = enc.Encode(payload)
return
}
writeJSON(w, result)
}