-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
96 lines (77 loc) · 2.17 KB
/
codec.go
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
package api
import (
"fmt"
"mime"
"net/http"
"github.com/RussellLuo/kun/pkg/httpcodec"
"github.com/RussellLuo/kun/pkg/werror"
"github.com/RussellLuo/kun/pkg/werror/gcode"
"github.com/RussellLuo/orchestrator"
"sigs.k8s.io/yaml/goyaml.v3"
)
type EventStream struct {
httpcodec.JSON
}
func (es *EventStream) EncodeSuccessResponse(w http.ResponseWriter, statusCode int, body interface{}) error {
output, ok := body.(*map[string]any)
if ok {
iterator, ok := orchestrator.Output(*output).Iterator()
if ok {
// Stream the response payload as events (i.e. Sever-Sent Events).
return es.sendEvents(w, statusCode, iterator)
}
}
return es.JSON.EncodeSuccessResponse(w, statusCode, body)
}
// sendEvents sends events to the client.
func (es *EventStream) sendEvents(w http.ResponseWriter, statusCode int, iterator *orchestrator.Iterator) error {
flusher, ok := w.(http.Flusher)
if !ok {
return fmt.Errorf("streaming unsupported")
}
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(statusCode)
flusher.Flush()
for result := range iterator.Next() {
if result.Err != nil {
return result.Err
}
// For simplicity, currently:
// - We assume that this iterator is from the HTTP response body.
// - We only send data-only events.
data, ok := result.Output["data"]
if !ok {
continue
}
fmt.Fprintf(w, "data: %s\n\n", data)
flusher.Flush()
}
return nil
}
type JSONOrYAML struct {
httpcodec.JSON
}
func (jy *JSONOrYAML) DecodeRequestBody(r *http.Request, out interface{}) error {
contentType := r.Header.Get("Content-Type")
mediaType := "application/json"
if contentType != "" {
var err error
mediaType, _, err = mime.ParseMediaType(contentType)
if err != nil {
return err
}
}
switch mediaType {
case "application/json":
return jy.JSON.DecodeRequestBody(r, out)
case "application/yaml":
if err := yaml.NewDecoder(r.Body).Decode(out); err != nil {
return werror.Wrap(gcode.ErrInvalidArgument, err)
}
default:
return werror.Wrapf(gcode.ErrInvalidArgument, "unsupported Media Type: %q", mediaType)
}
return nil
}