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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="https://dadi.tech/assets/products/dadi-web-full.png" alt="DADI Web" height="65"/>

[![npm (scoped)](https://img.shields.io/npm/v/@dadi/web.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/web)
[![coverage](https://img.shields.io/badge/coverage-74%25-yellow.svg?style=flat-square)](https://github.com/dadi/web)
[![coverage](https://img.shields.io/badge/coverage-76%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
[![Build Status](https://travis-ci.org/dadi/web.svg?branch=master)](https://travis-ci.org/dadi/web)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

Expand Down
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
4 changes: 2 additions & 2 deletions dadi/lib/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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) {
log.debug(
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 @@ -151,7 +151,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 @@ -346,7 +346,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 @@ -362,12 +362,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 @@ -448,7 +448,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
44 changes: 27 additions & 17 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 @@ -125,9 +123,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 @@ -142,6 +142,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 @@ -222,9 +225,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 @@ -248,14 +248,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 @@ -327,19 +327,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 Expand Up @@ -459,10 +468,11 @@ Server.prototype.loadApi = function (options, reload, callback) {
package: '@dadi/web',
version: version,
healthCheck: {
baseUrl: 'http://' +
config.get('server.host') +
':' +
config.get('server.port'),
baseUrl:
'http://' +
config.get('server.host') +
':' +
config.get('server.port'),
routes: config.get('status.routes')
}
}
Expand Down
8 changes: 4 additions & 4 deletions dadi/lib/providers/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ RemoteProvider.prototype.load = function (requestUrl, done) {

this.options = _.extend(this.options, headers)

this.makeRequest(done)
this.makeRequest(requestUrl, done)
})
})
}

RemoteProvider.prototype.makeRequest = function (done) {
RemoteProvider.prototype.makeRequest = function (requestUrl, done) {
debug(
'GET datasource "%s" %o',
this.datasource.schema.datasource.key,
Expand Down Expand Up @@ -158,9 +158,9 @@ RemoteProvider.prototype.makeRequest = function (done) {
this.options = _.extend(this.options, options)

debug('following %s redirect to %s', res.statusCode, res.headers.location)
this.makeRequest(done)
this.makeRequest(requestUrl, done)
} else {
this.handleResponse(res, done)
this.handleResponse(requestUrl, res, done)
}
})

Expand Down
Loading