-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
107 lines (92 loc) · 3.22 KB
/
handler.go
File metadata and controls
107 lines (92 loc) · 3.22 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
package schema
import (
"encoding/json"
"net/http"
"strings"
)
// RegisterRoutes registers the schema API endpoint on the given mux.
func RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/schema", HandleGetSchema)
}
// HandleGetSchema serves the workflow JSON schema.
func HandleGetSchema(w http.ResponseWriter, _ *http.Request) {
s := GenerateWorkflowSchema()
w.Header().Set("Content-Type", "application/schema+json")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(s); err != nil {
http.Error(w, "failed to encode schema", http.StatusInternalServerError)
}
}
// HandleSchemaAPI dispatches schema-related API requests. It handles:
// - /api/schema → workflow JSON schema
// - /api/v1/module-schemas → module config schemas (all or by type)
func HandleSchemaAPI(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
case strings.HasSuffix(path, "/module-schemas"):
HandleGetModuleSchemas(w, r)
default:
HandleGetSchema(w, r)
}
}
// SchemaService wraps the schema handlers as an http.Handler for delegate dispatch.
type SchemaService struct{}
// NewSchemaService creates a new SchemaService.
func NewSchemaService() *SchemaService {
return &SchemaService{}
}
// ServeHTTP implements http.Handler for config-driven delegate dispatch.
func (s *SchemaService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
seg := r.URL.Path
seg = strings.TrimRight(seg, "/")
last := seg[strings.LastIndex(seg, "/")+1:]
switch last {
case "modules":
HandleGetModuleSchemas(w, r)
default:
HandleGetSchema(w, r)
}
}
// moduleSchemaRegistry is the singleton registry used by the handler.
var moduleSchemaRegistry = NewModuleSchemaRegistry()
// GetModuleSchemaRegistry returns the global module schema registry,
// allowing callers to register additional schemas (e.g., custom module types).
func GetModuleSchemaRegistry() *ModuleSchemaRegistry {
return moduleSchemaRegistry
}
// stepSchemaRegistry is the singleton registry for step schemas.
var stepSchemaRegistry = NewStepSchemaRegistry()
// GetStepSchemaRegistry returns the global step schema registry,
// allowing callers to register additional schemas (e.g., plugin step types).
func GetStepSchemaRegistry() *StepSchemaRegistry {
return stepSchemaRegistry
}
// HandleGetModuleSchemas serves module config schemas.
// Query parameters:
// - type: return schema for a specific module type (e.g. ?type=http.server)
//
// Without ?type, returns all schemas as a map keyed by module type.
func HandleGetModuleSchemas(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
moduleType := r.URL.Query().Get("type")
if moduleType != "" {
s := moduleSchemaRegistry.Get(moduleType)
if s == nil {
http.Error(w, `{"error":"unknown module type"}`, http.StatusNotFound)
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(s); err != nil {
http.Error(w, "failed to encode schema", http.StatusInternalServerError)
}
return
}
// Return all schemas as a map
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(moduleSchemaRegistry.AllMap()); err != nil {
http.Error(w, "failed to encode schemas", http.StatusInternalServerError)
}
}