hello-lights 0.3.5 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -0
- package/lib/cli/serve/index.js +37 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,6 +99,34 @@ $ npm run cli -- serve --port 3000 # start the HTTP server on a custom port
|
|
|
99
99
|
|
|
100
100
|
Use `--help` for the full list of options, including `--serial-num` to target a specific device and `--selector multi` to control multiple traffic lights at once.
|
|
101
101
|
|
|
102
|
+
## HTTP Server REST API
|
|
103
|
+
|
|
104
|
+
The `serve` command starts an HTTP server that exposes the Commander interface as a REST API. By default it listens on port 9000.
|
|
105
|
+
|
|
106
|
+
| Endpoint | Method | Body | Response |
|
|
107
|
+
|---|---|---|---|
|
|
108
|
+
| `/run` | POST | Command string (plain text) | 202 Accepted |
|
|
109
|
+
| `/run?reset=true` | POST | Command string (plain text) | 202 Accepted (resets lights first) |
|
|
110
|
+
| `/cancel` | POST | — | 200 OK |
|
|
111
|
+
| `/definitions` | POST | Definition string (plain text) | 202 Accepted |
|
|
112
|
+
| `/commands` | GET | — | 200 + JSON array of command names |
|
|
113
|
+
| `/commands/:name` | GET | — | 200 + help text (`text/x-ansi`) or 404 |
|
|
114
|
+
| `/info` | GET | — | 200 + JSON array of `{ serialNum, status }` |
|
|
115
|
+
|
|
116
|
+
Examples:
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
$ curl -X POST http://localhost:9000/run -d 'blink 3 green 300'
|
|
120
|
+
$ curl -X POST http://localhost:9000/run?reset=true -d 'twinkle red 400'
|
|
121
|
+
$ curl -X POST http://localhost:9000/cancel
|
|
122
|
+
$ curl -X POST http://localhost:9000/definitions -d '(def foo (blink 1 green 300))'
|
|
123
|
+
$ curl http://localhost:9000/commands
|
|
124
|
+
$ curl http://localhost:9000/commands/turn
|
|
125
|
+
$ curl http://localhost:9000/info
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The server also serves a browser demo at the root URL (`/`).
|
|
129
|
+
|
|
102
130
|
## Running as a Linux Service
|
|
103
131
|
|
|
104
132
|
You can set up `hello-lights serve` to run automatically as a systemd user service on Linux.
|
package/lib/cli/serve/index.js
CHANGED
|
@@ -10,14 +10,44 @@ function createApp(commander) {
|
|
|
10
10
|
const app = express();
|
|
11
11
|
|
|
12
12
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
13
|
+
app.use(express.text({type: '*/*'}));
|
|
14
|
+
|
|
13
15
|
app.post('/run', (req, res) => {
|
|
14
|
-
let
|
|
15
|
-
req.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
let reset = req.query.reset === 'true';
|
|
17
|
+
commander.run(req.body || '', reset);
|
|
18
|
+
res.sendStatus(202);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
app.post('/cancel', (req, res) => {
|
|
22
|
+
commander.cancel();
|
|
23
|
+
res.sendStatus(200);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.post('/definitions', (req, res) => {
|
|
27
|
+
commander.runDefinitions(req.body || '');
|
|
28
|
+
res.sendStatus(202);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
app.get('/commands', (req, res) => {
|
|
32
|
+
res.json(commander.commandNames);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
app.get('/commands/:name', (req, res) => {
|
|
36
|
+
let command = commander.interpreter.lookup(req.params.name);
|
|
37
|
+
if (!command) {
|
|
38
|
+
res.status(404).send(`Command not found: "${req.params.name}"`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let helpText = commander.formatter.format(command.meta);
|
|
42
|
+
res.type('text/x-ansi').send(helpText);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
app.get('/info', (req, res) => {
|
|
46
|
+
if (commander.manager) {
|
|
47
|
+
res.json(commander.manager.info());
|
|
48
|
+
} else {
|
|
49
|
+
res.json([]);
|
|
50
|
+
}
|
|
21
51
|
});
|
|
22
52
|
|
|
23
53
|
return app;
|