Skip to content

Commit

Permalink
refactor middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Oenning committed Jun 1, 2016
1 parent 338fc0f commit 844d05c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 31 deletions.
9 changes: 1 addition & 8 deletions lib/middlewares/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ module.exports = {
},
mainMiddleware: function(f, handleRequest) {
return function(req, res, next) {
var enabled = f(req, res);

var respondWith = (res, result) => {
res.writeHead(result.status, { 'Content-Type': result.type });
res.end(result.body);
};

handleRequest(enabled, req, res, respondWith).then((handled) => {
handleRequest(f, req, res).then((handled) => {
if (!handled)
next();
}).catch(next);
Expand Down
8 changes: 1 addition & 7 deletions lib/middlewares/hapi.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
var onHapiRequest = function(f, handleRequest, request, reply) {
var enabled = f(request.raw.req, request.raw.res);

var respondWith = (res, result) => {
reply(result.body).type(result.type).code(result.status);
};

handleRequest(enabled, request.raw.req, request.raw.res, respondWith).then((handled) => {
handleRequest(f, request.raw.req, request.raw.res).then((handled) => {
if (!handled)
reply.continue();
});
Expand Down
10 changes: 1 addition & 9 deletions lib/middlewares/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ module.exports = {
},
mainMiddleware: function(f, handleRequest) {
return function *(next) {
var enabled = f(this.req, this.res);

var respondWith = (res, result) => {
this.status = result.status;
this.type = result.type;
this.body = result.body;
};

var handled = yield handleRequest(enabled, this.req, this.res, respondWith);
var handled = yield handleRequest(f, this.req, this.res);
if (!handled)
yield next;
};
Expand Down
13 changes: 6 additions & 7 deletions lib/miniprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ function getPath(req) {
return url.parse(req.url).pathname;
}

function handleRequest(enabled, req, res, respondWith) {
function handleRequest(f, req, res) {
return new Promise((resolve, reject) => {
var enabled = f(req, res);
var reqPath = getPath(req);

if(!reqPath.startsWith(resourcePath)){
Expand All @@ -95,18 +96,16 @@ function handleRequest(enabled, req, res, respondWith) {
}

if (!enabled) {
respondWith(res, {
type: 'text/plain; charset=utf-8',
status: 404,
body: 'MiniProfiler is disabled'
});
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('MiniProfiler is disabled');
resolve(true);
} else {
var segments = _.compact(reqPath.split('/'));
var lastPathSegment = segments[segments.length - 1];
var handler = (lastPathSegment == 'results') ? results : static;
handler(req, res, lastPathSegment, (result) => {
respondWith(res, result);
res.writeHead(result.status, { 'Content-Type': result.type });
res.end(result.body);
resolve(true);
});
}
Expand Down

0 comments on commit 844d05c

Please sign in to comment.