-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
61 lines (52 loc) · 1.61 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
56
57
58
59
60
61
var express = require('express');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var leasesParser = require('./modules/leasesParser');
var leaseFilePath = '/var/lib/dhcp/dhcpd.lease';
var inactive = false;
var hostname = 'localhost';
var port = '8080';
if (argv.i || argv.inactive) {
if (argv.i === true || argv.inactive === true) {
inactive = true;
} else {
console.error('Are you sure you want to show inactives?');
}
}
if (argv.l || argv.leaseFile) {
if ((argv.l && argv.l !== true) ||
(argv.leaseFile && argv.leaseFile !== true)) {
leaseFilePath = argv.l || argv.leaseFile;
} else {
console.error('Incorrect path to lease file');
}
}
if (argv.h || argv.hostname) {
if ((argv.h && argv.h !== true) ||
(argv.hostname && argv.hostname !== true)) {
hostname = (argv.h || argv.hostname).split(':');
port = hostname[1] || 8080;
hostname = hostname[0];
} else {
console.error('Incorrect hostname');
}
}
var app = express();
app.use(express.static(path.join(__dirname + '/public')));
app.get('/api/leases', function(req, res) {
leasesParser.getLeasesFromFile(leaseFilePath, function(err, leases) {
if (inactive) {
res.send(JSON.stringify(leases));
} else {
var activeLeases = leases.filter(function(lease) {
return lease.bindingState === 'active';
});
res.send(JSON.stringify(activeLeases));
}
});
});
var server = app.listen(port, hostname, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});