Skip to content

Commit

Permalink
⬆️ 1.1.0 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
IonicaBizau committed May 24, 2017
1 parent ce08500 commit 55a64f2
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 124 deletions.
253 changes: 131 additions & 122 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

const fileTree = require("fs-file-tree")
, path = require("path")
, forEach = require("iterate-object")
, typpy = require("typpy")
, upath = require("upath")
, httpMethods = require("methods")
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var fileTree = require("fs-file-tree"),
path = require("path"),
forEach = require("iterate-object"),
typpy = require("typpy"),
upath = require("upath"),
httpMethods = require("methods");

/**
* @name bloggify:init
Expand All @@ -21,140 +22,148 @@ const fileTree = require("fs-file-tree")
* - `bad_csrf` (String): The path the template that should be rendered when a bad CSRF token is sent to the server.
*
*/
module.exports = function bloggifyFlexibleRouter (config, Bloggify, cb) {
module.exports = function bloggifyFlexibleRouter(config, Bloggify, cb) {

const routesRoot = `${Bloggify.paths.root}/${config.routes_dir}`
, controllersRoot = `${Bloggify.paths.root}/${config.controllers_dir}`
;
var routesRoot = Bloggify.paths.root + "/" + config.routes_dir,
controllersRoot = Bloggify.paths.root + "/" + config.controllers_dir;

fileTree(routesRoot, (err, paths) => {
fileTree(routesRoot, function (err, paths) {
if (err) {
Bloggify.log(err, "warn");
} else {
let walk = (obj, file) => {
if (typpy(obj, Object)) {
if (obj.path && obj.stat) {
let parsed = path.parse(obj.path)
, fileName = parsed.name
, dir = parsed.dir
, relPath = upath.normalize(dir).replace(upath.normalize(routesRoot), "")
, uri = (relPath + "/" + (fileName === "index" ? "" : fileName)).replace(/\_/g, ":")
, controllerPath = controllersRoot + relPath + "/" + (fileName === "index" ? "" : fileName)
;
(function () {
var walk = function walk(obj, file) {
if (typpy(obj, Object)) {
if (obj.path && obj.stat) {
(function () {
var parsed = path.parse(obj.path),
fileName = parsed.name,
dir = parsed.dir,
relPath = upath.normalize(dir).replace(upath.normalize(routesRoot), ""),
uri = (relPath + "/" + (fileName === "index" ? "" : fileName)).replace(/\_/g, ":"),
controllerPath = controllersRoot + relPath + "/" + (fileName === "index" ? "" : fileName);

let hooks = null
, controller = null
;
var hooks = null,
controller = null;

try {
controller = require(controllerPath);
hooks = {};
if (typeof controller === "function") {
["all"].concat(httpMethods).forEach(c => hooks[c] = controller);
} else {
hooks = controller;
forEach(hooks, (fn, meth) => {
if (!httpMethods.includes(meth)) {
switch (meth) {
case "before":
case "after":
case "use":
Bloggify.server[meth](uri, fn)
break;
default:
Bloggify.log(`In ${controllerPath} you configured a method ${meth}, but that's not a supported HTTP method.`);
break;
}
try {
controller = require(controllerPath);
hooks = {};
if (typeof controller === "function") {
["all"].concat(httpMethods).forEach(function (c) {
return hooks[c] = controller;
});
} else {
hooks = controller;
forEach(hooks, function (fn, meth) {
if (!httpMethods.includes(meth)) {
switch (meth) {
case "before":
case "after":
case "use":
Bloggify.server[meth](uri, fn);
break;
default:
Bloggify.log("In " + controllerPath + " you configured a method " + meth + ", but that's not a supported HTTP method.");
break;
}
}
});
}
});
}
} catch (e) {
if (!e.message.includes(controllerPath)) {
Bloggify.log(e);
} else if (e.code !== "MODULE_NOT_FOUND") {
Bloggify.log(e);
}
}
} catch (e) {
if (!e.message.includes(controllerPath)) {
Bloggify.log(e);
} else if (e.code !== "MODULE_NOT_FOUND") {
Bloggify.log(e);
}
}

if (hooks) {
Bloggify.server.addPage(uri, ctx => {
let fn = hooks[ctx.method] || hooks.all;
if (fn) {
const done = (err, data) => {
if (err) {
return Bloggify.render(ctx, "500", {
error: err
, statusCode: 500
})
}
Bloggify.render(ctx, obj.path, data);
};
const maybePromise = fn(ctx, done);
if (maybePromise && typeof maybePromise.then) {
maybePromise.then(data => {
if (data === null) { return; }
process.nextTick(() => {
done(null, data);
});
}).catch(err => {
done(err);
});
}
} else {
Bloggify.render(ctx, obj.path);
}
});
} else {
Bloggify.server.addPage(uri, ctx => {
Bloggify.render(ctx, obj.path);
});
}
} else {
forEach(obj, walk)
}
}
};
walk(paths);
if (hooks) {
Bloggify.server.addPage(uri, function (ctx) {
var fn = hooks[ctx.method] || hooks.all;
if (fn) {
(function () {
var done = function done(err, data) {
if (err) {
return Bloggify.render(ctx, "500", {
error: err,
statusCode: 500
});
}
Bloggify.render(ctx, obj.path, data);
};
var maybePromise = fn(ctx, done);
if (maybePromise && _typeof(maybePromise.then)) {
maybePromise.then(function (data) {
if (data === null) {
return;
}
process.nextTick(function () {
done(null, data);
});
}).catch(function (err) {
done(err);
});
}
})();
} else {
Bloggify.render(ctx, obj.path);
}
});
} else {
Bloggify.server.addPage(uri, function (ctx) {
Bloggify.render(ctx, obj.path);
});
}
})();
} else {
forEach(obj, walk);
}
}
};
walk(paths);
})();
}
cb();
});

// Handle the error pages
if (config.error_pages) {
let obj = {};
let path404 = config.error_pages["404"];
let path500 = config.error_pages["500"];
let pathBadToken = config.error_pages.bad_csrf;
(function () {
var obj = {};
var path404 = config.error_pages["404"];
var path500 = config.error_pages["500"];
var pathBadToken = config.error_pages.bad_csrf;

if (path404) {
obj.notFound = ctx => {
Bloggify.render(ctx, `${routesRoot}/${path404}`, {
statusCode: 404
});
};
}

if (path500) {
obj.serverError = ctx => {
Bloggify.render(ctx, "500", {
statusCode: 500
});
};
Bloggify.viewer.registerTemplate("500", `${routesRoot}/${config.error_pages['500']}`, null, true);
}
if (path404) {
obj.notFound = function (ctx) {
Bloggify.render(ctx, routesRoot + "/" + path404, {
statusCode: 404
});
};
}

if (pathBadToken) {
obj.badCsrf = ctx => {
Bloggify.render(ctx, "bad-csrf-token", {
statusCode: 422
});
};
Bloggify.viewer.registerTemplate("bad-csrf-token", `${routesRoot}/${config.error_pages.bad_csrf}`, null, true);
}
if (path500) {
obj.serverError = function (ctx) {
Bloggify.render(ctx, "500", {
statusCode: 500
});
};
Bloggify.viewer.registerTemplate("500", routesRoot + "/" + config.error_pages['500'], null, true);
}

if (pathBadToken) {
obj.badCsrf = function (ctx) {
Bloggify.render(ctx, "bad-csrf-token", {
statusCode: 422
});
};
Bloggify.viewer.registerTemplate("bad-csrf-token", routesRoot + "/" + config.error_pages.bad_csrf, null, true);
}

Bloggify.server.errorPages(obj);
Bloggify.server.errorPages(obj);
})();
}

Bloggify.registerRouter(exports);
};
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"apps"
],
"license": "MIT",
"version": "1.0.10",
"version": "1.1.0",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down Expand Up @@ -49,4 +49,4 @@
"error_pages": {}
}
}
}
}

0 comments on commit 55a64f2

Please sign in to comment.