Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process routes after middleware #168

Merged
merged 7 commits into from
Jul 4, 2017
Merged
55 changes: 38 additions & 17 deletions dadi/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,42 +212,63 @@ Api.prototype.listener = function (req, res) {
req.params = {}
req.paths = []

// get matching routes, and add req.params
var matches = this._match(req)

var originalReqParams = req.params
var pathsLoaded = false

var doStack = function (i) {
return function (err) {
var doStack = stackIdx => {
return err => {
if (err) return errStack(0)(err)

// add the original params back, in case a middleware
// has modified the current req.params
_.extend(req.params, originalReqParams)

try {
self.stack[i](req, res, doStack(++i))
// if end of the stack, no middleware could handle the current
// request, so get matching routes from the loaded page components and
// add them to the stack after the cache handler but just before the
// 404 handler, then continue the loop
if (this.stack[stackIdx].name === 'cache' && !pathsLoaded) {
// find path specific handlers
var hrstart = process.hrtime()

var matches = this.getMatchingRoutes(req)

var hrend = process.hrtime(hrstart)
debug(
'getMatchingRoutes execution %ds %dms',
hrend[0],
hrend[1] / 1000000
)

if (!_.isEmpty(matches)) {
// add the matches after the cache middleware and before the final 404 handler
_.each(matches, match => {
this.stack.splice(-1, 0, match)
})
} else {
}

pathsLoaded = true
}

this.stack[stackIdx](req, res, doStack(++stackIdx))
} catch (e) {
return errStack(0)(e)
}
}
}

var self = this

var errStack = function (i) {
return function (err) {
self.errors[i](err, req, res, errStack(++i))
var errStack = stackIdx => {
return err => {
this.errors[stackIdx](err, req, res, errStack(++stackIdx))
}
}

// add path specific handlers
this.stack = this.stack.concat(matches)

// add 404 handler
// push the 404 handler
this.stack.push(notFound(this, req, res))

// start going through the middleware/routes
// start going through the middleware
doStack(0)()
}

Expand All @@ -274,7 +295,7 @@ Api.prototype.redirectListener = function (req, res) {
* @return {Array} handlers - the handlers that best matched the current URL
* @api private
*/
Api.prototype._match = function (req) {
Api.prototype.getMatchingRoutes = function (req) {
var path = url.parse(req.url).pathname
var handlers = []

Expand Down
6 changes: 3 additions & 3 deletions dadi/lib/api/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var compileTrust = function (val) {
}

middleware.handleHostHeader = function () {
return function (req, res, next) {
return function hostHeaderCheck (req, res, next) {
if (!req.headers.host || req.headers.host === '') {
res.statusCode = 400
return res.end()
Expand All @@ -45,7 +45,7 @@ middleware.handleHostHeader = function () {
}

middleware.setUpRequest = function () {
return function (req, res, next) {
return function populateRequest (req, res, next) {
Object.defineProperty(req, 'protocol', {
get: function () {
var protocol = config.get('server.protocol')
Expand Down Expand Up @@ -138,7 +138,7 @@ middleware.transportSecurity = function () {
log.info('Transport security is not enabled.')
}

return function (req, res, next) {
return function protocolRedirect (req, res, next) {
if (securityEnabled() && !req.secure) {
log.info('Redirecting insecure request for', req.url)
redirect(req, res, HTTPS)
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var log = require('@dadi/logger')

// This attaches middleware to the passed in app instance
module.exports = function (server) {
server.app.use(function (req, res, next) {
server.app.use(function authentication (req, res, next) {
log.info(
{ module: 'auth' },
'Retrieving access token for "' + req.url + '"'
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Cache.prototype.init = function () {
* @param {IncomingMessage} req - the current HTTP request
* @returns {object}
*/
this.server.app.use(function (req, res, next) {
this.server.app.use(function cache (req, res, next) {
var enabled = self.cachingEnabled(req)

debug('%s%s, cache enabled: %s', req.headers.host, req.url, enabled)
Expand Down
2 changes: 1 addition & 1 deletion dadi/lib/controller/forceDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var _ = require('underscore')
var url = require('url')

var forceDomain = function (options) {
return function (req, res, next) {
return function forceDomain (req, res, next) {
var protocol = req.headers['x-forwarded-proto'] || req.protocol || 'http'
var newRoute = domainRedirect(protocol, req.headers.host, req.url, options)
var statusCode
Expand Down
8 changes: 4 additions & 4 deletions dadi/lib/controller/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ module.exports = function (server, options) {
server.app.Router = new Router(server, options)

// middleware which blocks requests when we're too busy
server.app.use(function (req, res, next) {
server.app.use(function tooBusy (req, res, next) {
if (config.get('toobusy.enabled') && toobusy()) {
res.statusCode = 503
return res.end('HTTP Error 503 - Server Busy')
Expand All @@ -364,12 +364,12 @@ module.exports = function (server, options) {
rewriteFunction = rewrite(server.app.Router.rules)

// process rewrite rules first
server.app.use((req, res, next) => {
server.app.use(function rewrites (req, res, next) {
rewriteFunction(req, res, next)
})

// load rewrites from our DS and handle them
server.app.use((req, res, next) => {
server.app.use(function datasourceRewrites (req, res, next) {
debug('processing %s', req.url)

if (
Expand Down Expand Up @@ -451,7 +451,7 @@ module.exports = function (server, options) {
})

// handle generic url rewrite rules
server.app.use((req, res, next) => {
server.app.use(function configurableRewrites (req, res, next) {
debug('processing configurable rewrites %s', req.url)

var redirect = false
Expand Down
35 changes: 22 additions & 13 deletions dadi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ var Server = function () {
}

Server.prototype.start = function (done) {
var self = this

this.readyState = 2

var options = this.loadPaths()
Expand All @@ -75,7 +73,7 @@ Server.prototype.start = function (done) {
}

// override configuration variables based on request's host header
app.use((req, res, next) => {
app.use(function virtualHosts (req, res, next) {
var virtualHosts = config.get('virtualHosts')

if (_.isEmpty(virtualHosts)) {
Expand Down Expand Up @@ -143,9 +141,11 @@ Server.prototype.start = function (done) {

// add middleware for domain redirects
if (config.get('rewrites.forceDomain') !== '') {
var domain = config.get('rewrites.forceDomain')

app.use(
forceDomain({
hostname: config.get('rewrites.forceDomain'),
hostname: domain,
port: 80
})
)
Expand All @@ -160,6 +160,9 @@ Server.prototype.start = function (done) {
app.use(compress())
}

// request logging middleware
app.use(log.requestLogger)

// serve static files (css,js,fonts)
if (options.mediaPath) {
app.use(serveStatic(options.mediaPath, { index: false }))
Expand Down Expand Up @@ -240,9 +243,6 @@ Server.prototype.start = function (done) {
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))

// request logging middleware
app.use(log.requestLogger)

// session manager
var sessionConfig = config.get('sessions')

Expand All @@ -266,14 +266,14 @@ Server.prototype.start = function (done) {
}

// set up cache
var cacheLayer = cache(self)
var cacheLayer = cache(this)

// handle routing & redirects
router(self, options)
router(this, options)

if (config.get('api.enabled')) {
// authentication layer
auth(self)
auth(this)
}

// initialise the cache
Expand Down Expand Up @@ -345,19 +345,28 @@ Server.prototype.start = function (done) {
// do something when app is closing
process.on(
'exit',
this.exitHandler.bind(null, { server: this, cleanup: true })
this.exitHandler.bind(null, {
server: this,
cleanup: true
})
)

// catches ctrl+c event
process.on(
'SIGINT',
this.exitHandler.bind(null, { server: this, exit: true })
this.exitHandler.bind(null, {
server: this,
exit: true
})
)

// catches uncaught exceptions
process.on(
'uncaughtException',
this.exitHandler.bind(null, { server: this, exit: true })
this.exitHandler.bind(null, {
server: this,
exit: true
})
)
}

Expand Down
62 changes: 40 additions & 22 deletions scripts/coverage.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
#! /usr/bin/env node

var fs = require('fs');
var path = require('path');
var fs = require("fs")
var path = require("path")

var coberturaBadger = require('istanbul-cobertura-badger');
var coberturaBadger = require("istanbul-cobertura-badger")

var opts = {
badgeFileName: "coverage",
destinationDir: __dirname,
istanbulReportFile: path.resolve(__dirname + "/../coverage", "cobertura-coverage.xml"),
istanbulReportFile: path.resolve(
__dirname + "/../coverage",
"cobertura-coverage.xml"
),
thresholds: {
excellent: 90, // overall percent >= excellent, green badge
good: 60 // overall percent < excellent and >= good, yellow badge
// overall percent < good, red badge
}
};

//console.log(opts);
}

// Load the badge for the report$
coberturaBadger(opts, function parsingResults(err, badgeStatus) {
if (err) {
console.log("An error occurred: " + err.message);
console.log("An error occurred: " + err.message)
}

//console.log(badgeStatus);
// console.log(badgeStatus);

var readme = path.resolve(__dirname + '/../README.md');
var badgeUrl = badgeStatus.url; // e.g. http://img.shields.io/badge/coverage-60%-yellow.svg
var readme = path.resolve(__dirname + "/../README.md")
var badgeUrl = badgeStatus.url // e.g. http://img.shields.io/badge/coverage-60%-yellow.svg

// open the README.md and add this url
fs.readFile(readme, {encoding: 'utf-8'}, function (err, body) {
body = body.replace(/(!\[coverage\]\()(.+?)(\))/g, function(whole, a, b, c) {
return a + badgeUrl.replace('%', '%25') + '?style=flat-square' + c;
});
fs.readFile(readme, { encoding: "utf-8" }, function(err, body) {
var existingCoverage = body.match(/coverage-(\d+)/)[1]

fs.writeFile(readme, body, {encoding: 'utf-8'}, function (err) {
if (err) console.log(err.toString());
body = body.replace(/(!\[coverage\]\()(.+?)(\))/g, function(
whole,
a,
b,
c
) {
return a + badgeUrl.replace("%", "%25") + "?style=flat-square" + c
})

console.log("Coverage badge successfully added to " + readme);
});
})
console.log("Existing coverage:", existingCoverage + "%")
console.log("New Coverage:", badgeStatus.overallPercent + "%")
console.log(
badgeStatus.overallPercent < existingCoverage
? "Coverage check failed"
: "Coverage check passed"
)

if (badgeStatus.overallPercent < existingCoverage) {
process.exit(1)
}

//console.log(badgeStatus);
});
fs.writeFile(readme, body, { encoding: "utf-8" }, function(err) {
if (err) console.log(err.toString())

console.log("Coverage badge successfully added to " + readme)
})
})
})
1 change: 0 additions & 1 deletion test/acceptance/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe("Application", function() {
TestHelper.startServer(pages).then(() => {
client.get("/categories/Crime").end((err, res) => {
if (err) return done(err)

res.text.should.eql("<h3>Crime</h3>")
should.exist(res.headers["x-cache"])
res.headers["x-cache"].should.eql("MISS")
Expand Down
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--bail
--ui bdd
--recursive
--require=env-test
Expand Down
Loading