Skip to content

Commit

Permalink
Merge cb3ca38 into 6107af7
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbakker committed May 17, 2018
2 parents 6107af7 + cb3ca38 commit b94b2f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
48 changes: 47 additions & 1 deletion lib/install.js
Expand Up @@ -6,6 +6,11 @@ var _ = require('lodash');
var assert = require('assert-plus');
var vasync = require('vasync');
var verror = require('verror');
var compose = require('restify').helpers.compose;
var bunyan = require('bunyan');
var LOG = bunyan.createLogger({name: 'enroute'});



/**
* Install the enroute routes into the restify server.
Expand All @@ -32,6 +37,16 @@ function install(opts, cb) {
return cb1();
});

if (process.env.HOT_RELOAD) {
if (typeof process.env.HOT_RELOAD_BASEDIR !== 'undefined') {
LOG.info(
'Hot reloading of routes is enabled for base dir'
+ process.env.HOT_RELOAD_BASEDIR);
} else {
LOG.info('Hot reloading of routes is enabled.');
}
}

// go through each of the route names
_.forEach(opts.enroute.routes, function (methods, routeName) {
// go through each of the HTTP methods
Expand All @@ -45,7 +60,11 @@ function install(opts, cb) {
route = {
name: routeName,
method: method,
func: require(sourceFile)
func: process.env.HOT_RELOAD === 'true'
? function (req, resp, callback) {
reloadProxy(sourceFile, req, resp, callback);
}
: require(sourceFile)
};
} catch (e) {
return cb1(new verror.VError({
Expand Down Expand Up @@ -89,4 +108,31 @@ function install(opts, cb) {
});
}

function reloadProxy(sourceFile, req, resp, callback) {
if (typeof process.env.HOT_RELOAD_BASEDIR !== 'undefined') {
//Delete code loaded from a specific base dir
Object.keys(require.cache).forEach(function (cacheKey) {
if (cacheKey.indexOf(process.env.HOT_RELOAD_BASEDIR) !== -1) {
delete require.cache[cacheKey];
}
});
} else {
//Delete all cached entries
Object.keys(require.cache).forEach(function (cacheKey) {
delete require.cache[cacheKey];
});
}

try {
var handlers = require(sourceFile);
var handlerChain = compose(handlers);

handlerChain(req, resp, callback);
} catch (e) {
LOG.error('Uncaught error in route:');
LOG.error(e);
callback(e);
}
}

module.exports = install;
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -37,14 +37,15 @@
"jscs": "^3.0.7",
"mocha": "^3.1.2",
"nsp": "^2.6.2",
"restify": "^7.0.0",
"restify-clients": "^1.4.0",
"uuid": "^2.0.3"
},
"dependencies": {
"ajv": "^4.8.0",
"assert-plus": "^1.0.0",
"bunyan": "^1.8.12",
"lodash": "^4.16.4",
"restify": "^7.2.0",
"vasync": "^1.6.4",
"verror": "^1.6.0"
}
Expand Down

0 comments on commit b94b2f9

Please sign in to comment.