-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor route handling into single middleware module.
Tidy up some logging.
- Loading branch information
Yakov Khalinsky
committed
Feb 13, 2015
1 parent
4f36f94
commit bbe1a8b
Showing
7 changed files
with
124 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
var drakov = require('./lib/drakov'); | ||
|
||
exports.run = drakov.run; | ||
module.exports = { | ||
run: drakov.run, | ||
stop: drakov.stop, | ||
middleware: require('./lib/middleware') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
var glob = require('glob'); | ||
var fs = require('fs'); | ||
var protagonist = require('protagonist'); | ||
var async = require('async'); | ||
var pathToRegexp = require('path-to-regexp'); | ||
|
||
var urlParser = require('./url-parser'); | ||
var route = require('./route'); | ||
|
||
var ROUTE_MAP = null; | ||
|
||
var parseAction = function(uriTemplate, action) { | ||
var parsedUrl = urlParser.parse(uriTemplate); | ||
var key = parsedUrl.url; | ||
|
||
ROUTE_MAP[key] = ROUTE_MAP[key] || { urlExpression: parsedUrl.url, methods: {} }; | ||
ROUTE_MAP[key].methods[action.method] = ROUTE_MAP[key].methods[action.method] || []; | ||
|
||
var routeHandlers = route.getRouteHandlers(key, uriTemplate, action); | ||
Array.prototype.push.apply(ROUTE_MAP[key].methods[action.method], routeHandlers); | ||
}; | ||
|
||
var parseBlueprint = function(filePath) { | ||
return function(cb) { | ||
var data = fs.readFileSync(filePath, {encoding: 'utf8'}); | ||
protagonist.parse(data, function(err, result) { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
|
||
result.ast.resourceGroups.forEach(function(resourceGroup){ | ||
resourceGroup.resources.forEach(function(resource){ | ||
resource.actions.forEach(function(action){ | ||
parseAction(resource.uriTemplate, action); | ||
}); | ||
}); | ||
}); | ||
cb(); | ||
}); | ||
}; | ||
}; | ||
|
||
var setup = function(sourceFiles, cb) { | ||
ROUTE_MAP = {}; | ||
glob(sourceFiles, {} , function (err, files) { | ||
if (err) { | ||
console.error('Failed to parse contracts path.', err); | ||
process.exit(1); | ||
} | ||
|
||
var asyncFunctions = []; | ||
|
||
files.forEach(function(filePath) { | ||
asyncFunctions.push(parseBlueprint(filePath)); | ||
}); | ||
|
||
async.series(asyncFunctions, function(err) { | ||
cb(err); | ||
}); | ||
}); | ||
|
||
}; | ||
|
||
module.exports = function(sourceFiles) { | ||
var runMiddleware = function(req, res, next) { | ||
var handlers = null; | ||
Object.keys(ROUTE_MAP).forEach(function(urlPattern) { | ||
var regex = pathToRegexp(urlPattern); | ||
|
||
// req.path allows us to delegate query string handling to the route handler functions | ||
var match = regex.exec(req.path); | ||
if (match) { | ||
handlers = ROUTE_MAP[urlPattern].methods[req.method.toUpperCase()]; | ||
} | ||
}); | ||
|
||
if (handlers) { | ||
handlers.forEach(function(handler) { | ||
handler(req, res, next); | ||
}); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
return function(req, res, next) { | ||
if (!ROUTE_MAP) { | ||
setup(sourceFiles, function(err) { | ||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
runMiddleware(req, res, next); | ||
}); | ||
} else { | ||
runMiddleware(req, res, next); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters