-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
executable file
·58 lines (47 loc) · 1.84 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
#!/usr/bin/env node
const Koa = require('koa');
const serve = require('koa-static');
const logger = require('koa-logger');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { argv } = yargs(hideBin(process.argv));
// Version only comes in when run with NPM, so make this optional
const antennasVersion = process.env.npm_package_version ? `v${process.env.npm_package_version}` : '';
if (argv.nologo) {
console.log(`Antennas ${antennasVersion}`);
} else {
const logoFile = path.join('assets', 'logo.txt');
const logo = fs.readFileSync(path.resolve(__dirname, logoFile), 'utf8');
console.log('\x1b[34m%s\x1b[0m', logo, antennasVersion);
}
console.log('');
// Load config
const configHandler = require('./src/configHandler');
const config = argv.config ? configHandler.loadConfig(argv.config) : configHandler.loadConfig();
// Setup Antenna components
const device = require('./src/device')(config);
const router = require('./src/router')(config, device);
const ssdp = require('./src/ssdp');
// TODO: Figure out the discovery protocol UDP thing on port 65001
// Mainly, WHAT IS THAT YOU WANT PLEX?!
const app = new Koa();
try {
app
.use(logger())
.use(router.routes())
.use(router.allowedMethods())
.use(serve(path.resolve(__dirname, 'public'), { extensions: true }))
.use(async (ctx) => {
ctx.status = 404;
ctx.type = 'html';
ctx.body = fs.createReadStream(path.resolve(__dirname, path.join('public', '404.html')));
});
app.listen(5004);
console.log(`📡 Antennas are deployed! Proxying from ${config.antennas_url}`);
ssdp.broadcastSSDP(device);
} catch (e) {
console.log('❌ Antennas failed to deploy! 😮 It could be missing a config file, or something is misconfigured. See below for details:');
console.log(e);
}