forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
391 lines (346 loc) · 12.5 KB
/
api.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package api
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"github.com/bmizerany/pat"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/api/robeaux"
)
// API represents an API server
type API struct {
gobot *gobot.Gobot
router *pat.PatternServeMux
Host string
Port string
Cert string
Key string
handlers []func(http.ResponseWriter, *http.Request)
start func(*API)
}
// NewAPI returns a new api instance
func NewAPI(g *gobot.Gobot) *API {
return &API{
gobot: g,
router: pat.New(),
Port: "3000",
start: func(a *API) {
log.Println("Initializing API on " + a.Host + ":" + a.Port + "...")
http.Handle("/", a)
go func() {
if a.Cert != "" && a.Key != "" {
http.ListenAndServeTLS(a.Host+":"+a.Port, a.Cert, a.Key, nil)
} else {
log.Println("WARNING: API using insecure connection. " +
"We recommend using an SSL certificate with Gobot.")
http.ListenAndServe(a.Host+":"+a.Port, nil)
}
}()
},
}
}
// ServeHTTP calls api handlers and then serves request using api router
func (a *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
for _, handler := range a.handlers {
rec := httptest.NewRecorder()
handler(rec, req)
for k, v := range rec.Header() {
res.Header()[k] = v
}
if rec.Code == http.StatusUnauthorized {
http.Error(res, "Not Authorized", http.StatusUnauthorized)
return
}
}
a.router.ServeHTTP(res, req)
}
// Post wraps api router Post call
func (a *API) Post(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Post(path, http.HandlerFunc(f))
}
// Put wraps api router Put call
func (a *API) Put(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Put(path, http.HandlerFunc(f))
}
// Delete wraps api router Delete call
func (a *API) Delete(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Del(path, http.HandlerFunc(f))
}
// Options wraps api router Options call
func (a *API) Options(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Options(path, http.HandlerFunc(f))
}
// Get wraps api router Get call
func (a *API) Get(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Get(path, http.HandlerFunc(f))
}
// Head wraps api router Head call
func (a *API) Head(path string, f func(http.ResponseWriter, *http.Request)) {
a.router.Head(path, http.HandlerFunc(f))
}
// AddHandler appends handler to api handlers
func (a *API) AddHandler(f func(http.ResponseWriter, *http.Request)) {
a.handlers = append(a.handlers, f)
}
// Start initializes the api by setting up c3pio routes and robeaux
func (a *API) Start() {
mcpCommandRoute := "/api/commands/:command"
robotDeviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
robotCommandRoute := "/api/robots/:robot/commands/:command"
a.Get("/api/commands", a.mcpCommands)
a.Get(mcpCommandRoute, a.executeMcpCommand)
a.Post(mcpCommandRoute, a.executeMcpCommand)
a.Get("/api/robots", a.robots)
a.Get("/api/robots/:robot", a.robot)
a.Get("/api/robots/:robot/commands", a.robotCommands)
a.Get(robotCommandRoute, a.executeRobotCommand)
a.Post(robotCommandRoute, a.executeRobotCommand)
a.Get("/api/robots/:robot/devices", a.robotDevices)
a.Get("/api/robots/:robot/devices/:device", a.robotDevice)
a.Get("/api/robots/:robot/devices/:device/events/:event", a.robotDeviceEvent)
a.Get("/api/robots/:robot/devices/:device/commands", a.robotDeviceCommands)
a.Get(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Post(robotDeviceCommandRoute, a.executeRobotDeviceCommand)
a.Get("/api/robots/:robot/connections", a.robotConnections)
a.Get("/api/robots/:robot/connections/:connection", a.robotConnection)
a.Get("/api/", a.mcp)
a.Get("/", func(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/index.html", http.StatusMovedPermanently)
})
a.Get("/index.html", a.robeaux)
a.Get("/images/:a", a.robeaux)
a.Get("/js/:a", a.robeaux)
a.Get("/js/:a/", a.robeaux)
a.Get("/js/:a/:b", a.robeaux)
a.Get("/css/:a", a.robeaux)
a.Get("/css/:a/", a.robeaux)
a.Get("/css/:a/:b", a.robeaux)
a.Get("/partials/:a", a.robeaux)
a.start(a)
}
// robeaux returns handler for robeaux routes.
// Writes asset in response and sets correct header
func (a *API) robeaux(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path
buf, err := robeaux.Asset(path[1:])
if err != nil {
http.Error(res, err.Error(), http.StatusNotFound)
return
}
t := strings.Split(path, ".")
if t[len(t)-1] == "js" {
res.Header().Set("Content-Type", "text/javascript; charset=utf-8")
} else if t[len(t)-1] == "css" {
res.Header().Set("Content-Type", "text/css; charset=utf-8")
}
res.Write(buf)
}
// mcp returns MCP route handler.
// Writes JSON with gobot representation
func (a *API) mcp(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"MCP": gobot.NewJSONGobot(a.gobot)}, res)
}
// mcpCommands returns commands route handler.
// Writes JSON with global commands representation
func (a *API) mcpCommands(res http.ResponseWriter, req *http.Request) {
a.writeJSON(map[string]interface{}{"commands": gobot.NewJSONGobot(a.gobot).Commands}, res)
}
// robots returns route handler.
// Writes JSON with robots representation
func (a *API) robots(res http.ResponseWriter, req *http.Request) {
jsonRobots := []*gobot.JSONRobot{}
a.gobot.Robots().Each(func(r *gobot.Robot) {
jsonRobots = append(jsonRobots, gobot.NewJSONRobot(r))
})
a.writeJSON(map[string]interface{}{"robots": jsonRobots}, res)
}
// robot returns route handler.
// Writes JSON with robot representation
func (a *API) robot(res http.ResponseWriter, req *http.Request) {
if robot, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.writeJSON(map[string]interface{}{"robot": robot}, res)
}
}
// robotCommands returns commands route handler
// Writes JSON with robot commands representation
func (a *API) robotCommands(res http.ResponseWriter, req *http.Request) {
if robot, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.writeJSON(map[string]interface{}{"commands": robot.Commands}, res)
}
}
// robotDevices returns devices route handler.
// Writes JSON with robot devices representation
func (a *API) robotDevices(res http.ResponseWriter, req *http.Request) {
if robot := a.gobot.Robot(req.URL.Query().Get(":robot")); robot != nil {
jsonDevices := []*gobot.JSONDevice{}
robot.Devices().Each(func(d gobot.Device) {
jsonDevices = append(jsonDevices, gobot.NewJSONDevice(d))
})
a.writeJSON(map[string]interface{}{"devices": jsonDevices}, res)
} else {
a.writeJSON(map[string]interface{}{"error": "No Robot found with the name " + req.URL.Query().Get(":robot")}, res)
}
}
// robotDevice returns device route handler.
// Writes JSON with robot device representation
func (a *API) robotDevice(res http.ResponseWriter, req *http.Request) {
if device, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":device")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.writeJSON(map[string]interface{}{"device": device}, res)
}
}
func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
f, _ := res.(http.Flusher)
c, _ := res.(http.CloseNotifier)
dataChan := make(chan string)
closer := c.CloseNotify()
res.Header().Set("Content-Type", "text/event-stream")
res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive")
if event := a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).(gobot.Eventer).
Event(req.URL.Query().Get(":event")); event != nil {
gobot.On(event, func(data interface{}) {
d, _ := json.Marshal(data)
dataChan <- string(d)
})
for {
select {
case data := <-dataChan:
fmt.Fprintf(res, "data: %v\n\n", data)
f.Flush()
case <-closer:
log.Println("Closing connection")
return
}
}
} else {
a.writeJSON(map[string]interface{}{
"error": "No Event found with the name " + req.URL.Query().Get(":event"),
}, res)
}
}
// robotDeviceCommands returns device commands route handler
// writes JSON with robot device commands representation
func (a *API) robotDeviceCommands(res http.ResponseWriter, req *http.Request) {
if device, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":device")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.writeJSON(map[string]interface{}{"commands": device.Commands}, res)
}
}
// robotConnections returns connections route handler
// writes JSON with robot connections representation
func (a *API) robotConnections(res http.ResponseWriter, req *http.Request) {
jsonConnections := []*gobot.JSONConnection{}
if robot := a.gobot.Robot(req.URL.Query().Get(":robot")); robot != nil {
robot.Connections().Each(func(c gobot.Connection) {
jsonConnections = append(jsonConnections, gobot.NewJSONConnection(c))
})
a.writeJSON(map[string]interface{}{"connections": jsonConnections}, res)
} else {
a.writeJSON(map[string]interface{}{"error": "No Robot found with the name " + req.URL.Query().Get(":robot")}, res)
}
}
// robotConnection returns connection route handler
// writes JSON with robot connection representation
func (a *API) robotConnection(res http.ResponseWriter, req *http.Request) {
if conn, err := a.jsonConnectionFor(req.URL.Query().Get(":robot"), req.URL.Query().Get(":connection")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.writeJSON(map[string]interface{}{"connection": conn}, res)
}
}
// executeMcpCommand calls a global command asociated to requested route
func (a *API) executeMcpCommand(res http.ResponseWriter, req *http.Request) {
a.executeCommand(a.gobot.Command(req.URL.Query().Get(":command")),
res,
req,
)
}
// executeRobotDeviceCommand calls a device command asociated to requested route
func (a *API) executeRobotDeviceCommand(res http.ResponseWriter, req *http.Request) {
if _, err := a.jsonDeviceFor(req.URL.Query().Get(":robot"),
req.URL.Query().Get(":device")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.executeCommand(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Device(req.URL.Query().Get(":device")).(gobot.Commander).
Command(req.URL.Query().Get(":command")),
res,
req,
)
}
}
// executeRobotCommand calls a robot command asociated to requested route
func (a *API) executeRobotCommand(res http.ResponseWriter, req *http.Request) {
if _, err := a.jsonRobotFor(req.URL.Query().Get(":robot")); err != nil {
a.writeJSON(map[string]interface{}{"error": err.Error()}, res)
} else {
a.executeCommand(
a.gobot.Robot(req.URL.Query().Get(":robot")).
Command(req.URL.Query().Get(":command")),
res,
req,
)
}
}
// executeCommand writes JSON response with `f` returned value.
func (a *API) executeCommand(f func(map[string]interface{}) interface{},
res http.ResponseWriter,
req *http.Request,
) {
body := make(map[string]interface{})
json.NewDecoder(req.Body).Decode(&body)
if f != nil {
a.writeJSON(map[string]interface{}{"result": f(body)}, res)
} else {
a.writeJSON(map[string]interface{}{"error": "Unknown Command"}, res)
}
}
// writeJSON writes `j` as JSON in response
func (a *API) writeJSON(j interface{}, res http.ResponseWriter) {
data, _ := json.Marshal(j)
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
// Debug add handler to api that prints each request
func (a *API) Debug() {
a.AddHandler(func(res http.ResponseWriter, req *http.Request) {
log.Println(req)
})
}
func (a *API) jsonRobotFor(name string) (jrobot *gobot.JSONRobot, err error) {
if robot := a.gobot.Robot(name); robot != nil {
jrobot = gobot.NewJSONRobot(robot)
} else {
err = errors.New("No Robot found with the name " + name)
}
return
}
func (a *API) jsonDeviceFor(robot string, name string) (jdevice *gobot.JSONDevice, err error) {
if device := a.gobot.Robot(robot).Device(name); device != nil {
jdevice = gobot.NewJSONDevice(device)
} else {
err = errors.New("No Device found with the name " + name)
}
return
}
func (a *API) jsonConnectionFor(robot string, name string) (jconnection *gobot.JSONConnection, err error) {
if connection := a.gobot.Robot(robot).Connection(name); connection != nil {
jconnection = gobot.NewJSONConnection(connection)
} else {
err = errors.New("No Connection found with the name " + name)
}
return
}