1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var debug = require("debug")("jefferson");
|
7 |
|
8 | module.exports = function (app, conf) {
|
9 | if (!app) {
|
10 | throw new Error("app parameter must be supplied");
|
11 | }
|
12 | if (!conf) {
|
13 | throw new Error("application configuration must be supplied");
|
14 | }
|
15 |
|
16 | var proxies = conf.proxies;
|
17 |
|
18 | var possiblyProxy = function (middleware) {
|
19 | if (!proxies || !proxies.length) {
|
20 | return middleware;
|
21 | }
|
22 | return middleware.map(function (mw) {
|
23 | var lastProxy = proxies[proxies.length - 1].init(mw);
|
24 | for (var i = proxies.length - 2; i >= 0; i--) {
|
25 | lastProxy = proxies[i].init(lastProxy);
|
26 | }
|
27 | return lastProxy;
|
28 | });
|
29 | };
|
30 |
|
31 | var wireRoute = function (routeName, route) {
|
32 | var method = route.method.toLowerCase(),
|
33 | path = route.path,
|
34 | middleware = possiblyProxy(route.middleware);
|
35 |
|
36 | debug("routing " + routeName + " - " + method + " " + path + " - " + middleware.length + " middlewares");
|
37 |
|
38 | app[method](path, middleware);
|
39 | };
|
40 | var wireRoutes = function () {
|
41 | var routeNames = Object.keys(conf.routes);
|
42 |
|
43 | debug("wiring " + routeNames.length + " routes");
|
44 |
|
45 | routeNames.forEach(function (routeName) {
|
46 | return wireRoute(routeName, conf.routes[routeName]);
|
47 | });
|
48 | };
|
49 |
|
50 | wireRoutes();
|
51 | }; |
\ | No newline at end of file |