From 3113ba154ac726f7ce09b8ef8ef95dd07972b5c4 Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Tue, 30 Aug 2016 17:38:47 -0700
Subject: [PATCH] Move configuration reading to as early as possible

Should reduce situations where requests arrive before locks are registered, putting daemon into restart loop.
---
 server.js | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/server.js b/server.js
index 5238019..9356c7c 100755
--- a/server.js
+++ b/server.js
@@ -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() );
-- 
GitLab