-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
55 lines (45 loc) · 1.48 KB
/
app.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
//Parse commandline arguments
const args = require('./argparser.js');
const streamer = require('./streamer.js');
const express = require('express');
const http = require('http');
const helmet = require('helmet');
// Import authenticator
let authenticator;
try {
authenticator = require('./authenticator.js');
} catch (exception) {
console.warn('No authenticator found, default authenticator will be using', exception.message);
}
// Init express.app
const app = express();
app.set('port', args.port || process.env.PORT || 3005);
app.use(helmet());
//initialize a simple http server
const server = http.createServer(app);
app.use(function (req, res, next) {
if (args.origin) {
res.setHeader('Access-Control-Allow-Origin', args.origin);
}
next();
});
//Serve html page
app.get('/stream/logs', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
function default_authenticator({ username, password, hostname }) {
return new Promise((resolve) => {
process.nextTick(() => {
if (args.username !== username || args.password !== password || (args.origin && !hostname.includes(args.origin))) {
return resolve(false);
}
return resolve(true);
});
});
}
//initialize the WebSocket server instance
streamer({ server, authenticate: authenticator || default_authenticator });
//start our server
server.listen(app.get('port'), () => {
console.log(`Server started on port ${app.get('port')} :)`);
});