This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathserver.js
53 lines (46 loc) · 1.48 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var config = require('./config.js')
, worker = require.resolve('./worker.js')
, cluster = require('cluster')
, clusterConf = config.cluster || {}
clusterConf.exec = worker
process.stdout.on('error', function (er) {
if (er.code === 'EPIPE') return
throw er
})
// set up the server cluster.
var clusterMaster = require("cluster-master")
, npm = require("npm")
config.log.master = true
var logger = require('bunyan').createLogger(config.log)
console.error = logger.warn.bind(logger)
console.log = logger.info.bind(logger)
var npmconf = config.npm || {}
npmconf["node-version"] = null
npm.load(npmconf, function (er) {
if (er) throw er
// Ok, we're ready! Spin up the cluster.
clusterMaster(clusterConf)
})
// if there's an http port, then redirect from there to https.
// this almost never needs to change, so just run it in the master,
// rather than having each worker to it.
if (config.https && config.httpPort) {
var h = 'https://' + config.host
if (config.port && config.port !== 443) h += ':' + config.port
var canon = config.canon = require('canonical-host')(h, 301)
var http = require('http')
httpServer = http.createServer(function (req, res) {
if (canon(req, res)) return
// wtf?
res.statusCode = 400
res.end('bad')
})
httpServer.listen(config.httpPort)
// restart on crashes
httpServer.on('close', function () {
console.error('http->https redirector crashed')
setTimeout(function () {
httpServer.listen(config.httpPort)
}, 100)
})
}