Move configuration reading to as early as possible

Should reduce situations where requests arrive before locks are registered, putting daemon into restart loop.
parent 1b6d0f99
......@@ -13,6 +13,10 @@ var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } );
var cache = apicache.middleware;
var request = require( 'request' );
/**
* CONFIGURATION
*/
var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
var serverConfig = require( process.env.AUGUSTCTL_SERVER_CONFIG || './server-config.json' );
......@@ -23,6 +27,23 @@ var port = serverConfig.port || 3000;
var app = express();
app.use( morgan( DEBUG ? 'dev' : 'combined' ) );
// Parse lock configurations
Object.keys( config ).forEach( function( lockName ) {
var lockConfig = config[ lockName ];
console.log( 'Loading config for lock "%s" (%s)', lockName, lockConfig.lockUuid );
augustctl.scan( lockConfig.lockUuid ).then( function( peripheral ) {
var lock = new augustctl.Lock(
peripheral,
lockConfig.offlineKey,
lockConfig.offlineKeyOffset
);
app.set( 'lock' + lockName, lock );
} );
} );
/**
* UTILITIES
*/
......@@ -189,23 +210,6 @@ app.get( '/api/disconnect/:lock_name', function( req, res, next ) {
* SERVER SETUP
*/
// Parse lock configurations
Object.keys( config ).forEach( function( lockName ) {
var lockConfig = config[ lockName ];
console.log( 'Loading config for lock "%s" (%s)', lockName, lockConfig.lockUuid );
augustctl.scan( lockConfig.lockUuid ).then( function( peripheral ) {
var lock = new augustctl.Lock(
peripheral,
lockConfig.offlineKey,
lockConfig.offlineKeyOffset
);
app.set( 'lock' + lockName, lock );
} );
} );
// Start Express server
var server = app.listen( port, address, function() {
console.log( 'Listening at %j', server.address() );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment