This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (128 loc) · 4.19 KB
/
index.js
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
const { program } = require('commander');
var pjson = require('./package.json');
const fs = require("fs");
function myParseInt(value, dummyPrevious) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) throw new Error('Invalid argument: Must be a number.');
return parsedValue;
}
program.option("-p, --port <number>", "start http server with a specific port number", myParseInt, 21889);
program.option("-c, --connect <id>", "connect to an existing obs server instead of starting a new one");
program.option("-v, --version", "output the version number");
program.option("-vo, --osn-version", "output the osn version number");
program.option("--obs <path>", "the path to obs lib, if at a non-standard location");
program.option("--data <path>", "the path to data folder, if at a non-standard location");
program.parse();
var clio = program.opts();
if (clio.version) {
console.log(pjson.version);
return;
}
if (clio.osnVersion) {
console.log(pjson.osnVersion);
return;
}
console.log("Received CLI arguments: " + JSON.stringify(clio));
const _ = require("lodash");
const express = require("express");
const bodyParser = require('body-parser');
const obs = require("./obs");
const volmeter = require("./volmeter");
// create express
const app = express();
const port = clio.port;
let server;
// handle sigint/shutdown
if (process.platform === "win32") {
const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", function () {
console.log("Recieved SIGINT");
shutdown(0);
});
process.on("SIGTERM", function () {
console.log("Recieved SIGTERM");
shutdown(0);
});
function shutdown(code) {
console.log("Start graceful shutdown: code " + code);
try {
obs.release();
server.close();
} catch { }
console.log("Exiting...");
process.exit(code);
}
app.use(bodyParser.json({ extended: true }));
app.use(function (req, res, next) {
res.setHeader('Content-Type', 'application/json');
next();
});
function DO_OK(res) {
res.status(200).send(JSON.stringify({ status: "ok" }));
}
function DO_JSON(res, body) {
res.status(200).send(JSON.stringify(body));
}
// routes
app.get("/", (req, res) => {
const routes = _(app._router.stack)
.filter(s => !_.isEmpty(s.route))
.map(s => ({ route: s.route.path, methods: s.route.methods }))
.toArray();
DO_JSON(res, routes);
});
app.get("/status", (req, res) => {
DO_JSON(res, obs.getStatistics());
});
app.get("/audio/speakers", (req, res) => {
DO_JSON(res, volmeter.getSpeakers());
});
app.get("/audio/microphones", (req, res) => {
DO_JSON(res, volmeter.getMicrophones());
});
app.get("/settings/:settingKey", (req, res) => {
let small = true;
if (_.isString(req.query.detailed) && req.query.detailed.toLowerCase() === "true") {
small = false;
}
DO_JSON(res, obs.getSettingsCategory(req.params.settingKey, small));
});
app.post("/settings/:settingKey", (req, res) => {
obs.updateSettingsCategory(req.params.settingKey, req.body);
DO_OK(res);
});
app.post("/recording/start", (req, res, next) => {
obs.recordingStart(req.body).then(s => DO_OK(res)).catch(next);
});
app.post("/recording/stop", (req, res, next) => {
obs.recordingStop().then(s => DO_OK(res)).catch(next);
});
app.post("/shutdown", (req, res) => {
shutdown(0);
});
app.use(function (err, req, res, next) {
console.error(err.stack)
if (!!err.message) {
res.status(500).send(JSON.stringify({ status: "error", message: err.message /*, stack: err.stack*/ }));
} else {
res.status(500).send(JSON.stringify({ status: "error", message: err }));
}
});
// startup
try {
obs.init(clio.connect, clio.obs, clio.data);
server = app.listen(port, "localhost", () => {
console.log(`Listening at http://localhost:${port}`);
});
volmeter.createVolmeterServer(server);
} catch (e) {
console.log(e.message || e);
shutdown(1);
}