-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhandler.go
More file actions
117 lines (98 loc) · 3.07 KB
/
handler.go
File metadata and controls
117 lines (98 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package matchspec
import (
"encoding/json"
"net/http"
"github.com/greynewell/mist-go/protocol"
)
// Handler provides HTTP handlers for the MatchSpec API.
type Handler struct {
runner *Runner
registry *SuiteRegistry
}
// NewHandler creates a handler wired to the given runner.
func NewHandler(runner *Runner, registry *SuiteRegistry) *Handler {
return &Handler{runner: runner, registry: registry}
}
// Ingest handles POST /mist — accepts MIST protocol messages containing
// evaluation runs and returns results.
func (h *Handler) Ingest(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var msg protocol.Message
if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
http.Error(w, "invalid message: "+err.Error(), http.StatusBadRequest)
return
}
if msg.Type != protocol.TypeEvalRun {
http.Error(w, "expected type eval.run, got "+msg.Type, http.StatusBadRequest)
return
}
var run protocol.EvalRun
if err := msg.Decode(&run); err != nil {
http.Error(w, "invalid run payload: "+err.Error(), http.StatusBadRequest)
return
}
results, err := h.runner.Run(r.Context(), run)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
}
// RunDirect handles POST /eval — accepts a direct EvalRun JSON body.
func (h *Handler) RunDirect(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var run protocol.EvalRun
if err := json.NewDecoder(r.Body).Decode(&run); err != nil {
http.Error(w, "invalid request: "+err.Error(), http.StatusBadRequest)
return
}
results, err := h.runner.Run(r.Context(), run)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
}
// SuitesResponse is the JSON body for GET /suites.
type SuitesResponse struct {
Suites []SuiteInfo `json:"suites"`
}
// SuiteInfo describes a registered suite.
type SuiteInfo struct {
Name string `json:"name"`
TaskCount int `json:"task_count"`
}
// Suites handles GET /suites — lists all registered suites.
func (h *Handler) Suites(w http.ResponseWriter, r *http.Request) {
var resp SuitesResponse
for _, name := range h.registry.Names() {
if s, ok := h.registry.Get(name); ok {
resp.Suites = append(resp.Suites, SuiteInfo{
Name: s.Name,
TaskCount: len(s.Tasks),
})
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}
// Results handles GET /results — returns all collected results.
func (h *Handler) Results(w http.ResponseWriter, r *http.Request) {
suite := r.URL.Query().Get("suite")
var results []protocol.EvalResult
if suite != "" {
results = h.runner.ResultsBySuite(suite)
} else {
results = h.runner.Results()
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
}