-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (56 loc) · 1.47 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
59
60
61
62
63
64
require('dotenv').load();
var connect = require('connect'),
http = require('http'),
api = process.env.API || 'http://localhost:3000/',
Serial = require('./libs/serial'),
env = process.env,
port = env.PORT || 3000,
createKeys = process.argv[2] === 'create',
io = require('./libs/sdk')( env.API ),
app = connect( ),
emptyUsers = [];
router = require('./libs/router')( require('./libs/routes') ),
serial = new Serial( env.SERIALPORT, {
auth : env.SERIALAUTH
}),
home = new ( require('./libs/home') )( env.DBDIR, serial, io );
if ( env.NODE_ENV === 'production' ) {
app.use(function( req, res, next ) {
var auth = req.headers.authorization;
if ( auth && auth === env.SECRETAUTH ) {
return next();
}
res.statusCode = 401;
res.end('Not Authorized');
});
}
app
.use(connect.bodyParser())
.use(connect.query())
.use(function( req, res, next ){
req.home = home;
next();
})
.use(router);
serial.on('user:granted', function ( user ) {
var opts = {
rfid : user.id
};
console.log( user.id + ' access granted' );
// send a checkin
io.users.checkin( opts, function( err, res ){
if ( err ) return;
console.log( 'sync with api ' + res.success );
});
});
serial.on('auth', function( isGood ) {
if ( !isGood ) return;
home.syncUsers( function ( ) {
console.log("Users synced");
});
});
serial.on('user:denied', function ( token ) {
console.log( token, 'denied' );
});
http.createServer( app ).listen( port );
console.log("server listening on " + port );