Created
March 1, 2016 05:19
-
-
Save teux/709aaef89e812b316899 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var cluster = require('cluster'); | |
var bootable = require('bootable'); | |
var bootableEnv = require('bootable-environment'); | |
var spdy = require('spdy'); | |
var keys = require('spdy-keys'); | |
var koa = require('koa'); | |
var app; | |
if (cluster.isMaster) { | |
cluster.fork(); | |
cluster.on('listening', function (worker) { | |
worker.on('exit', function () { | |
console.error('Worker ' + worker.process.pid + ' died, forking new one'); | |
cluster.fork(); | |
}); | |
}); | |
} else { | |
app = bootable(koa()); | |
app.keys = ['some secret']; | |
// Configure for NODE_ENV, make application initialization, attach middleware and routes | |
app.phase(bootableEnv('config/environments')); | |
app.phase(bootable.initializers(__dirname + '/config/initializers')); | |
app.phase(bootable.initializers(__dirname + '/config/middleware')); | |
/**√ | |
* Creates ready for SPDY server. | |
*/ | |
app.phase(function () { | |
var host = this.config.host; | |
var port = this.config.port; | |
var secure = this.config.secure; | |
// For plain HTTP use `{plain: true, ssl: false}` instead `keys`. | |
// Also server instance is stored in `_server` member for webpack middleware. | |
var server = this._server = secure ? | |
spdy.createServer(keys, this.callback()) : | |
spdy.createServer({plain: true, ssl: false}, this.callback()); | |
server.listen(port, host, function(err) { | |
if (err) return; | |
console.info('HTTP server listening on http%s://%s:%d (%s)', | |
secure ? 's' : '', host, port, process.pid); | |
}); | |
process.on('uncaughtException', function (err) { | |
console.error('UNCAUGHT EXCEPTION:', err.stack); | |
server.close(function () { | |
process.exit(1); | |
}); | |
setTimeout(function () { | |
process.exit(1); //exit anyway after 2s | |
}, 2000); | |
}.bind(this)); | |
}); | |
app.boot(function(err) { | |
if (err) throw err; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment