Cache clearing and different durations for different endpoints

parent 61e0cfa7
......@@ -5,7 +5,7 @@ var express = require( 'express' );
var morgan = require( 'morgan' );
var await = require( 'asyncawait/await' );
var async = require( 'asyncawait/async' );
var apicache = require( 'apicache' ).options( { defaultDuration: 30000 } );
var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } );
var cache = apicache.middleware;
var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
......@@ -25,86 +25,113 @@ var ret = {
'msg': ''
};
// Endpoint to perform all lock actions
app.get( '/api/:lock_action/:lock_name', cache( '5 seconds' ), function( req, res ) {
// Endpoint to check lock status
app.get( '/api/status/:lock_name', cache( '15 seconds' ), function( req, res ) {
// Parse allowed request arguments
var lock = app.get( 'lock' + req.params.lock_name );
if ( ! lock ) {
clear_caches( req.params.lock_name );
res.sendStatus( 400 );
return;
}
// Suspendable functions to check lock's status
var actionFunction = async( function() {
var status = await( lock.status() );
ret.ret = status;
ret.status = statusStringtoInt( status );
ret.msg = "Status checked successfully.";
lock.disconnect();
res.json( ret );
} );
// Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err );
lock.disconnect();
clear_caches( req.params.lock_name );
res.sendStatus( 500 );
} );
} );
// Endpoint to change lock state
app.get( '/api/:lock_action(lock|unlock)/:lock_name', cache( '5 seconds' ), function( req, res ) {
// Parse allowed request arguments
var action = req.params.lock_action,
allowedActions = [ 'unlock', 'lock', 'status' ];
allowedActions = [ 'unlock', 'lock' ];
if ( -1 === allowedActions.indexOf( action ) ) {
res.sendStatus( 400 );
return;
}
var lock = app.get( 'lock' + req.params.lock_name );
var lockName = req.params.lock_name,
lock = app.get( 'lock' + lockName );
if ( ! lock ) {
clear_caches( lockName );
res.sendStatus( 400 );
return;
}
// Suspendable functions to interact with lock based on requested action
if ( 'status' === action ) {
// Checks lock's state and returns it
var actionFunction = async( function() {
var status = await( lock.status() ),
statusInt = -1;
if ( 'locked' === status ) {
statusInt = 0;
} else if ( 'unlocked' === status ) {
statusInt = 1;
}
var actionFunction = async( function() {
var status = await( lock.status() );
if ( 'lock' === action && 'unlocked' === status ) {
var cmd = await( lock.forceLock() );
ret.ret = 'locked';
ret.status = 0;
ret.msg = 'Locked as requested.';
} else if ( 'unlock' === action && 'locked' === status ) {
var cmd = await( lock.forceUnlock() );
ret.ret = 'unlocked';
ret.status = 1;
ret.msg = 'Unlocked as requested.';
} else {
ret.ret = status;
ret.status = statusInt;
ret.msg = "Status checked successfully.";
lock.disconnect();
res.json( ret );
} );
} else {
// Locks or unlocks a requested lock, if not already in that state
var actionFunction = async( function() {
var status = await( lock.status() );
if ( 'lock' === action && 'unlocked' === status ) {
var cmd = await( lock.forceLock() );
ret.ret = 'locked';
ret.status = 0;
ret.msg = 'Locked as requested.';
} else if ( 'unlock' === action && 'locked' === status ) {
var cmd = await( lock.forceUnlock() );
ret.ret = 'unlocked';
ret.status = 1;
ret.msg = 'Unlocked as requested.';
} else {
var statusInt = -1;
if ( 'locked' === status ) {
statusInt = 0;
} else if ( 'unlocked' === status ) {
statusInt = 1;
}
ret.ret = status;
ret.status = statusInt;
ret.msg = "No change made. Lock was already '" + status + "'.";
}
lock.disconnect();
res.json( ret );
} );
}
ret.status = statusStringtoInt( status );
ret.msg = "No change made. Lock was already '" + status + "'.";
}
lock.disconnect();
clear_caches( lockName );
res.json( ret );
} );
// Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err );
lock.disconnect();
clear_caches( lockName );
res.sendStatus( 500 );
} );
} );
// Convert named status to integer representation
function statusStringtoInt( status ) {
var statusInt = -1;
if ( 'locked' === status ) {
statusInt = 0;
} else if ( 'unlocked' === status ) {
statusInt = 1;
}
return statusInt;
}
// Clear cached routes
function clear_caches( lockName ) {
apicache.clear( '/api/status/' + lockName );
apicache.clear( '/api/lock/' + lockName );
apicache.clear( '/api/unlock/' + lockName );
return true;
}
// Parse lock configurations
Object.keys( config ).forEach( function( lockName ) {
var lockConfig = config[ lockName ];
......
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