dynamic loading configured endpoint
This commit is contained in:
parent
0d0f564475
commit
da0a827e7e
@ -1,3 +1,7 @@
|
||||
{
|
||||
"httpPort":8080
|
||||
"httpPort":8080,
|
||||
"debugLevel":"debug",
|
||||
"endpoints": {
|
||||
"library":"endpoint/library.js"
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,8 @@
|
||||
*/
|
||||
var Action = require('../action/action.js');
|
||||
|
||||
function Endpoint(path) {
|
||||
function Endpoint() {
|
||||
// constructor
|
||||
this.path = path;
|
||||
};
|
||||
Endpoint.prototype = {
|
||||
/**
|
||||
|
10
lib/endpoint/library.js
Normal file
10
lib/endpoint/library.js
Normal file
@ -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();
|
@ -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 <string> path
|
||||
* @param path <string> full path
|
||||
* @param callback <function(error, endpoint)> 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
|
||||
|
Loading…
Reference in New Issue
Block a user