Skip to content
Snippets Groups Projects
Verified Commit 9d433092 authored by Erick Hitter's avatar Erick Hitter
Browse files

Cache clearing and different durations for different endpoints

parent 61e0cfa7
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ var express = require( 'express' ); ...@@ -5,7 +5,7 @@ var express = require( 'express' );
var morgan = require( 'morgan' ); var morgan = require( 'morgan' );
var await = require( 'asyncawait/await' ); var await = require( 'asyncawait/await' );
var async = require( 'asyncawait/async' ); var async = require( 'asyncawait/async' );
var apicache = require( 'apicache' ).options( { defaultDuration: 30000 } ); var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } );
var cache = apicache.middleware; var cache = apicache.middleware;
var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' ); var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
...@@ -25,86 +25,113 @@ var ret = { ...@@ -25,86 +25,113 @@ var ret = {
'msg': '' 'msg': ''
}; };
// Endpoint to perform all lock actions // Endpoint to check lock status
app.get( '/api/:lock_action/:lock_name', cache( '5 seconds' ), function( req, res ) { 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 // Parse allowed request arguments
var action = req.params.lock_action, var action = req.params.lock_action,
allowedActions = [ 'unlock', 'lock', 'status' ]; allowedActions = [ 'unlock', 'lock' ];
if ( -1 === allowedActions.indexOf( action ) ) { if ( -1 === allowedActions.indexOf( action ) ) {
res.sendStatus( 400 ); res.sendStatus( 400 );
return; return;
} }
var lock = app.get( 'lock' + req.params.lock_name ); var lockName = req.params.lock_name,
lock = app.get( 'lock' + lockName );
if ( ! lock ) { if ( ! lock ) {
clear_caches( lockName );
res.sendStatus( 400 ); res.sendStatus( 400 );
return; return;
} }
// Suspendable functions to interact with lock based on requested action // Suspendable functions to interact with lock based on requested action
if ( 'status' === action ) { var actionFunction = async( function() {
// Checks lock's state and returns it var status = await( lock.status() );
var actionFunction = async( function() {
var status = await( lock.status() ), if ( 'lock' === action && 'unlocked' === status ) {
statusInt = -1; var cmd = await( lock.forceLock() );
if ( 'locked' === status ) { ret.ret = 'locked';
statusInt = 0; ret.status = 0;
} else if ( 'unlocked' === status ) { ret.msg = 'Locked as requested.';
statusInt = 1; } 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.ret = status;
ret.status = statusInt; ret.status = statusStringtoInt( status );
ret.msg = "Status checked successfully."; ret.msg = "No change made. Lock was already '" + status + "'.";
}
lock.disconnect();
res.json( ret ); lock.disconnect();
} ); clear_caches( lockName );
} else { res.json( ret );
// 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 );
} );
}
// Perform requested action // Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) { lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err ); console.error( err );
lock.disconnect(); lock.disconnect();
clear_caches( lockName );
res.sendStatus( 500 ); 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 // Parse lock configurations
Object.keys( config ).forEach( function( lockName ) { Object.keys( config ).forEach( function( lockName ) {
var lockConfig = config[ lockName ]; var lockConfig = config[ lockName ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment