-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_comms.go
More file actions
123 lines (113 loc) · 3.45 KB
/
api_comms.go
File metadata and controls
123 lines (113 loc) · 3.45 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
118
119
120
121
122
123
package control
import (
"encoding/json"
"net/http"
"github.com/zsiec/switchframe/server/control/httperr"
"github.com/zsiec/switchframe/server/internal"
)
// commsJoinRequest is the JSON body for joining the comms channel.
type commsJoinRequest struct {
OperatorID string `json:"operatorId"`
Name string `json:"name"`
}
// commsLeaveRequest is the JSON body for leaving the comms channel.
type commsLeaveRequest struct {
OperatorID string `json:"operatorId"`
}
// commsMuteRequest is the JSON body for setting mute state.
type commsMuteRequest struct {
OperatorID string `json:"operatorId"`
Muted bool `json:"muted"`
}
// registerCommsRoutes registers comms-related API routes on the given mux.
func (a *API) registerCommsRoutes(mux *http.ServeMux) {
if a.commsMgr == nil {
return
}
mux.HandleFunc("POST /api/comms/join", a.handleCommsJoin)
mux.HandleFunc("POST /api/comms/leave", a.handleCommsLeave)
mux.HandleFunc("PUT /api/comms/mute", a.handleCommsMute)
mux.HandleFunc("GET /api/comms/status", a.handleCommsStatus)
}
func (a *API) handleCommsJoin(w http.ResponseWriter, r *http.Request) {
if a.commsMgr == nil {
httperr.Write(w, http.StatusNotImplemented, "comms not enabled")
return
}
a.setLastOperator(r)
var req commsJoinRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.OperatorID == "" {
httperr.Write(w, http.StatusBadRequest, "operatorId required")
return
}
if req.Name == "" {
httperr.Write(w, http.StatusBadRequest, "name required")
return
}
if err := a.commsMgr.Join(req.OperatorID, req.Name); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
a.broadcast()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.enrichedState())
}
func (a *API) handleCommsLeave(w http.ResponseWriter, r *http.Request) {
if a.commsMgr == nil {
httperr.Write(w, http.StatusNotImplemented, "comms not enabled")
return
}
a.setLastOperator(r)
var req commsLeaveRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.OperatorID == "" {
httperr.Write(w, http.StatusBadRequest, "operatorId required")
return
}
a.commsMgr.Leave(req.OperatorID)
a.broadcast()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.enrichedState())
}
func (a *API) handleCommsMute(w http.ResponseWriter, r *http.Request) {
if a.commsMgr == nil {
httperr.Write(w, http.StatusNotImplemented, "comms not enabled")
return
}
a.setLastOperator(r)
var req commsMuteRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.OperatorID == "" {
httperr.Write(w, http.StatusBadRequest, "operatorId required")
return
}
if err := a.commsMgr.SetMuted(req.OperatorID, req.Muted); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
a.broadcast()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.enrichedState())
}
func (a *API) handleCommsStatus(w http.ResponseWriter, _ *http.Request) {
if a.commsMgr == nil {
httperr.Write(w, http.StatusNotImplemented, "comms not enabled")
return
}
state := a.commsMgr.State()
if state == nil {
state = &internal.CommsState{}
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(state)
}