-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (58 loc) · 1.67 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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const expressWs = require('express-ws')(app);
const DemoBase = require("./demobase");
const port = 8080;
global.__basedir = __dirname;
// Main web interface
app.use(express.static(`${global.__basedir}/public`))
app.use(express.static(`${global.__basedir}/node_modules`))
app.use(bodyParser.json())
// Hold demos
let demos = [];
global.__demosDir = (process.argv.length === 2) ? __dirname + "/demos" : process.argv[2];
// Load demos
function loadDemos() {
demos = [];
// Probably not best way to dynamically load modules
try {
fs.readdirSync(global.__demosDir, {withFileTypes: true}).forEach((location) => {
// Only look at directories
if (location.isDirectory) {
let demo = new DemoBase(app, global.__demosDir + "/" +location.name);
demo.ws = expressWs.getWss('/');
demos.push(demo);
}
});
}
catch {
console.warn("Unable to load demos directory.");
}
}
// Load demos
loadDemos();
// API Endpoint returns list of demos
app.get("/getDemoList", (req, res) => {
let demoList = [];
demos.forEach((demo) => {
demoList.push(demo.config);
})
res.send(demoList);
});
// Reload demos list
app.get("/reloadDemoList", (req, res) => {
loadDemos();
let demoList = [];
demos.forEach((demo) => {
demoList.push(demo.config);
})
res.send(demoList);
});
app.ws('/', (ws, req) => {
});
// Start server
app.listen(port, () => {
console.log(`Demo Web Interface listening on port ${port}`)
});