Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hot reload functionality #13

Merged
merged 1 commit into from May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 51 additions & 1 deletion lib/install.js
Expand Up @@ -6,6 +6,9 @@ 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;

/**
* Install the enroute routes into the restify server.
Expand All @@ -23,6 +26,12 @@ function install(opts, cb) {
assert.object(opts.server, 'opts.server');
assert.string(opts.basePath, 'opts.basePath');

if (typeof opts.log !== 'undefined') {
LOG = opts.log.child({ component: 'enroute' });
} else {
LOG = bunyan.createLogger({name: 'enroute'});
}

vasync.pipeline({arg: {}, funcs: [
// Read the routes from disk and parse them as functions
function getRoutes(ctx, cb1) {
Expand All @@ -32,6 +41,15 @@ function install(opts, cb) {
return cb1();
});

if (opts.enroute.hotReload) {
if (typeof opts.basePath !== 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulbakker one qq - in what cases was basePath not defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an assumption that it’s possibly not defined. If this option is always required we can go without the check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks! It's always required, so I think we can simply the check. 👍

LOG.info({basedir: opts.basePath},
'Hot reloading of routes is enabled for base dir');
} 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 +63,12 @@ function install(opts, cb) {
route = {
name: routeName,
method: method,
func: require(sourceFile)
func: opts.enroute.hotReload
? function (req, resp, callback) {
reloadProxy(sourceFile, req,
resp, callback, opts);
}
: require(sourceFile)
};
} catch (e) {
return cb1(new verror.VError({
Expand Down Expand Up @@ -89,4 +112,31 @@ function install(opts, cb) {
});
}

function reloadProxy(sourceFile, req, resp, callback, opts) {
if (typeof opts.basePath !== 'undefined') {
//Delete code loaded from a specific base dir
Object.keys(require.cache).forEach(function (cacheKey) {
if (cacheKey.indexOf(opts.basePath) !== -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: 3 additions & 0 deletions lib/schemas.js
Expand Up @@ -99,6 +99,9 @@ module.exports = {
},
schemaVersion: {
type: 'number'
},
hotReload: {
type: 'boolean'
}
},
additionalProperties: false,
Expand Down
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we use child loggers I don't think we'll need bunyan anymore. What's restify needed for? Or is that really a devDependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restify exports the new helpers.compose function to create a handler chain, which is used in the reloadProxy function.

"vasync": "^1.6.4",
"verror": "^1.6.0"
}
Expand Down