Browse Source

split app from server, add toobusy

pull/7/head
Peter Johnson 10 years ago
parent
commit
794db085a2
  1. 24
      app.js
  2. 26
      index.js
  3. 19
      middleware/toobusy.js
  4. 3
      package.json

24
app.js

@ -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 );

26
index.js

@ -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') );
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 );
cluster(app)
.use(cluster.stats())
.listen( process.env.PORT || 3100 );

19
middleware/toobusy.js

@ -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;

3
package.json

@ -33,7 +33,8 @@
"dependencies": {
"express": "^4.8.8",
"geopipes-elasticsearch-backend": "0.0.7",
"pelias-esclient": "0.0.25"
"pelias-esclient": "0.0.25",
"toobusy": "^0.2.4"
},
"devDependencies": {
"ciao": "^0.3.4",

Loading…
Cancel
Save