-
Notifications
You must be signed in to change notification settings - Fork 46
/
server.js
executable file
·91 lines (75 loc) · 2.48 KB
/
server.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
#!/usr/bin/env node
'use strict'
const url = require('url')
const http = require('http')
const debug = require('debug')('airplanejs')
const getPort = require('get-port')
const opn = require('opn')
const patterns = require('patterns')()
const rtlsdr = require('./lib/rtlsdr')
const routes = require('./lib/routes')
process.on('SIGINT', exit)
const argv = require('minimist')(process.argv.slice(2))
if (argv.help || argv.h) {
help()
process.exit()
}
if (argv.version || argv.v) {
console.log(require('./package').version)
process.exit()
}
// Start radio
rtlsdr.start(argv)
patterns.add('GET /', routes.index)
patterns.add('GET /assets/{file}', routes.assets)
patterns.add('GET /airlines', routes.airlines)
patterns.add('GET /airports', routes.airports)
patterns.add('GET /routes', routes.routes)
patterns.add('GET /aircrafts', routes.aircrafts)
const server = http.createServer(function (req, res) {
debug('%s %s', req.method, req.url)
const path = url.parse(req.url).pathname
const match = patterns.match(req.method + ' ' + path)
if (!match) {
res.writeHead(404)
res.end()
return
}
const fn = match.value // expects the value to be a function
req.params = match.params
fn(req, res)
})
const customPort = argv.port || argv.p
if (customPort) listen(customPort)
else getPort({port: 3000}).then(listen)
function listen (port) {
server.listen(port, function () {
const url = 'http://localhost:' + port
if (argv.browser === false) {
console.log('Server running at: %s', url)
} else {
console.log('Opening %s in your favorite browser...', url)
opn(url)
}
})
}
function help () {
console.log('Usage:')
console.log(' airplanejs [options]')
console.log()
console.log('Options:')
console.log(' --help -h Show this help')
console.log(' --version -v Output AirplaneJS version')
console.log(' --device -d <index> Select RTL dongle (default: 0)')
console.log(' --frequency -f <hz> Set custom frequency (default: 1090000000)')
console.log(' --gain -g <gain> Set custom tuner gain')
console.log(' --auto-gain Disable manual tuner gain (default: off)')
console.log(' --enable-agc Use Automatic Gain Control (default: off)')
console.log(' --port -p <port> Set custom HTTP server port (default: 3000)')
console.log(' --no-browser Disable automatic opening of default browser')
}
function exit () {
console.log('Closing down RTL-SDR device...')
rtlsdr.stop()
process.exit()
}