From 61e0cfa7964c06ec17e08e3b301244bf5aea87aa Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Sat, 27 Aug 2016 18:40:18 -0700
Subject: [PATCH] Initial implementation of caching, to prevent lock DDoS and
 the need to restart the server

---
 package.json | 3 ++-
 server.js    | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/package.json b/package.json
index b31d70d..5859ea2 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,8 @@
     "express": "^4.13.3",
     "morgan": "^1.6.1",
     "noble": "^1.1.0",
-    "asyncawait": "^0.7.4"
+    "asyncawait": "^0.7.4",
+    "apicache": "*"
   },
   "repository": {
     "type": "git",
diff --git a/server.js b/server.js
index bd03e6b..6986666 100755
--- a/server.js
+++ b/server.js
@@ -5,6 +5,8 @@ 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 cache     = apicache.middleware;
 
 var config       = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
 var serverConfig = require( process.env.AUGUSTCTL_SERVER_CONFIG || './server-config.json' );
@@ -24,7 +26,7 @@ var ret = {
 };
 
 // Endpoint to perform all lock actions
-app.get( '/api/:lock_action/:lock_name', function( req, res ) {
+app.get( '/api/:lock_action/:lock_name', cache( '5 seconds' ), function( req, res ) {
     // Parse allowed request arguments
     var action = req.params.lock_action,
         allowedActions = [ 'unlock', 'lock', 'status' ];
-- 
GitLab