-
Notifications
You must be signed in to change notification settings - Fork 36
/
server.js
executable file
·43 lines (35 loc) · 994 Bytes
/
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
'use strict';
var augustctl = require('./index');
var express = require('express');
var morgan = require('morgan');
var config = require(process.env.AUGUSTCTL_CONFIG || './config.json');
var DEBUG = process.env.NODE_ENV !== 'production';
var address = config.address || 'localhost';
var port = config.port || 3000;
var app = express();
app.use(morgan(DEBUG ? 'dev' : 'combined'));
app.get('/api/unlock', function(req, res) {
var lock = app.get('lock');
if (!lock) {
res.sendStatus(503);
return;
}
lock.connect().then(function() {
return lock.unlock();
}).disposer(function() {
lock.disconnect();
// TODO: report errors
res.sendStatus(204);
});
});
augustctl.scan(config.lockUuid).then(function(peripheral) {
var lock = new augustctl.Lock(
peripheral,
config.offlineKey,
config.offlineKeyOffset
);
app.set('lock', lock);
});
var server = app.listen(port, address, function() {
console.log('Listening at %j', server.address());
});