From da0a827e7e16554534f5d5fcc4e16f7f138a2d97 Mon Sep 17 00:00:00 2001 From: Meutel Date: Mon, 20 Jan 2014 20:30:45 +0100 Subject: [PATCH] dynamic loading configured endpoint --- config/config.json | 6 +++++- lib/endpoint/endpoint.js | 3 +-- lib/endpoint/library.js | 10 ++++++++++ lib/router/router.js | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 lib/endpoint/library.js diff --git a/config/config.json b/config/config.json index caa6943..739bbd5 100644 --- a/config/config.json +++ b/config/config.json @@ -1,3 +1,7 @@ { - "httpPort":8080 + "httpPort":8080, + "debugLevel":"debug", + "endpoints": { + "library":"endpoint/library.js" + } } diff --git a/lib/endpoint/endpoint.js b/lib/endpoint/endpoint.js index 42ff608..dc03ed8 100644 --- a/lib/endpoint/endpoint.js +++ b/lib/endpoint/endpoint.js @@ -3,9 +3,8 @@ */ var Action = require('../action/action.js'); -function Endpoint(path) { +function Endpoint() { // constructor - this.path = path; }; Endpoint.prototype = { /** diff --git a/lib/endpoint/library.js b/lib/endpoint/library.js new file mode 100644 index 0000000..428d3c5 --- /dev/null +++ b/lib/endpoint/library.js @@ -0,0 +1,10 @@ +/** + * Endpoint library. + */ +var Endpoint = require('./endpoint.js'); +function Library() { + Endpoint.call(this); +} +Library.prototype = Object.create(Endpoint.prototype, { +}); +exports = module.exports = new Library(); diff --git a/lib/router/router.js b/lib/router/router.js index 7dc0460..f6f6902 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -6,6 +6,8 @@ var util = require('util') Endpoint = require('../endpoint/endpoint.js'), Outputter = require('../outputter/outputter'); +var PATH_RE=/\/([a-zA-Z0-9]+)(?:\/|$)([a-zA-Z0-9]+)?/; + exports = module.exports = Router; function Router() { // constructor @@ -18,13 +20,37 @@ Router.prototype = { /** * Build the endpoint for given path. - * @param path path + * @param path full path * @param callback callback */ buildEndpoint : function (path, callback) { logger.debug('Building endpoint for ' + path); - // TODO - callback(null, new Endpoint(path)); + + var endpoint = null; + var error = null; + if (!config.endpoints) { + error = new Error("no endpoints defined"); + } else { + var match = PATH_RE.exec(path); + var resName = match ? match[1] : null; + logger.debug('Match resource : ' + resName); + + var resModule = config.endpoints[resName]; + if (resModule) { + logger.debug('Loading ' + resModule); + try { + logger.debug('../' + resModule); + endpoint = require('../' + resModule); + } catch (err) { + logger.debug('Error loading module'); + error = err; + } + } else { + error = new Error('No module for ' + resName); + } + + } + callback(error, endpoint); }, /** @@ -67,6 +93,10 @@ Router.prototype = { // TODO sanitize url.pathname this.buildEndpoint(url.pathname, function(err, endpoint) { //TODO err + if (err) { + logger.error(err); + // TODO write error in req + } //TODO endpoint.buildAction(req.method, url, function(err, action) { //TODO err