mirror of https://github.com/pelias/api.git
Peter Johnson
10 years ago
4 changed files with 51 additions and 21 deletions
@ -0,0 +1,24 @@ |
|||||||
|
|
||||||
|
var app = require('express')(); |
||||||
|
|
||||||
|
/** ----------------------- middleware ----------------------- **/ |
||||||
|
|
||||||
|
app.use( require('./middleware/toobusy') ); // should be first
|
||||||
|
app.use( require('./middleware/headers') ); |
||||||
|
app.use( require('./middleware/cors') ); |
||||||
|
app.use( require('./middleware/jsonp') ); |
||||||
|
|
||||||
|
/** ----------------------- routes ----------------------- **/ |
||||||
|
|
||||||
|
// api root
|
||||||
|
app.get( '/', require('./controller/index') ); |
||||||
|
|
||||||
|
// suggest API
|
||||||
|
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); |
||||||
|
|
||||||
|
/** ----------------------- error middleware ----------------------- **/ |
||||||
|
|
||||||
|
app.use( require('./middleware/404') ); |
||||||
|
app.use( require('./middleware/500') ); |
||||||
|
|
||||||
|
app.listen( process.env.PORT || 3100 ); |
@ -1,23 +1,9 @@ |
|||||||
|
|
||||||
var app = require('express')(); |
/** cluster webserver across all cores **/ |
||||||
|
|
||||||
/** ----------------------- middleware ----------------------- **/ |
var cluster = require('cluster'), |
||||||
|
app = require('./app'); |
||||||
|
|
||||||
app.use( require('./middleware/headers') ); |
cluster(app) |
||||||
app.use( require('./middleware/cors') ); |
.use(cluster.stats()) |
||||||
app.use( require('./middleware/jsonp') ); |
.listen( process.env.PORT || 3100 ); |
||||||
|
|
||||||
/** ----------------------- routes ----------------------- **/ |
|
||||||
|
|
||||||
// api root
|
|
||||||
app.get( '/', require('./controller/index') ); |
|
||||||
|
|
||||||
// suggest API
|
|
||||||
app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); |
|
||||||
|
|
||||||
/** ----------------------- error middleware ----------------------- **/ |
|
||||||
|
|
||||||
app.use( require('./middleware/404') ); |
|
||||||
app.use( require('./middleware/500') ); |
|
||||||
|
|
||||||
app.listen( process.env.PORT || 3100 ); |
|
@ -0,0 +1,19 @@ |
|||||||
|
|
||||||
|
// middleware which blocks requests when the eventloop is too busy
|
||||||
|
var toobusy = require('toobusy'); |
||||||
|
|
||||||
|
function middleware(req, res, next){ |
||||||
|
if( toobusy() ){ |
||||||
|
res.status(503); // Service Unavailable
|
||||||
|
return next('Server Overwhelmed'); |
||||||
|
} |
||||||
|
return next(); |
||||||
|
} |
||||||
|
|
||||||
|
// calling .shutdown allows your process to exit normally
|
||||||
|
process.on('SIGINT', function() { |
||||||
|
toobusy.shutdown(); |
||||||
|
process.exit(); |
||||||
|
}); |
||||||
|
|
||||||
|
module.exports = middleware; |
Loading…
Reference in new issue