Live video on the AT Protocol
1package misttriggers
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/golang/glog"
9 "stream.place/streamplace/pkg/errors"
10)
11
12type StreamSourcePayload struct {
13 StreamName string
14}
15
16func ParseStreamSourcePayload(payload MistTriggerBody) (StreamSourcePayload, error) {
17 lines := payload.Lines()
18 if len(lines) != 1 {
19 return StreamSourcePayload{}, fmt.Errorf("expected 1 line in STREAM_SOURCE payload but got %d. Payload: %s", len(lines), payload)
20 }
21
22 return StreamSourcePayload{
23 StreamName: lines[0],
24 }, nil
25}
26
27func (d *MistCallbackHandlersCollection) TriggerStreamSource(ctx context.Context, w http.ResponseWriter, req *http.Request, body MistTriggerBody) {
28 payload, err := ParseStreamSourcePayload(body)
29 if err != nil {
30 glog.Infof("Error parsing STREAM_SOURCE payload error=%q payload=%q", err, string(body))
31 errors.WriteHTTPBadRequest(w, "Error parsing STREAM_SOURCE payload", err)
32 return
33 }
34 resp, err := d.broker.TriggerStreamSource(ctx, &payload)
35 if err != nil {
36 glog.Infof("Error handling STREAM_SOURCE payload error=%q payload=%q", err, string(body))
37 errors.WriteHTTPInternalServerError(w, "Error handling STREAM_SOURCE payload", err)
38 return
39 }
40 // Flushing necessary here for Mist to handle an empty response body
41 flusher := w.(http.Flusher)
42 flusher.Flush()
43 w.Write([]byte(resp)) // nolint:errcheck
44}