-
Notifications
You must be signed in to change notification settings - Fork 32
/
server.js
67 lines (56 loc) · 1.97 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
var HatchPlatform = require('./lib/hatch').HatchPlatform;
var createServer = require('compound').createServer;
var cluster = require('cluster');
var fs = require('fs');
var path = require('path');
// check the app has been installed fully
if (!checkInstall()) {
return;
}
// set the hatch path environment variables
process.env.HATCH_WIDGETCONTROLLERPATH = __dirname + '/app/controllers/widget';
process.env.HATCH_PATH = __dirname;
/**
* Server module exports method which returns new instance of application server
*
* @param {Object} params - railway/express webserver initialization params.
* @returns CompoundJS powered express webserver
*/
var app = module.exports = function getServerInstance(params) {
params = params || {};
// specify current dir as default root of server
params.root = params.root || __dirname;
var app = createServer(params);
app.compound.hatch = new HatchPlatform(app);
return app;
};
if (!module.parent) {
var port = process.env.PORT || 3000;
var host = process.env.HOST || '0.0.0.0';
var server = app();
if (cluster.isMaster && process.env.CLUSTER) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
console.log('Starting Hatch cluster with %d threads', cpuCount);
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
} else {
server.listen(port, host, function () {
console.log(
'Hatch server listening on %s:%d within %s environment',
host, port, server.set('env')
);
});
}
}
// function to check required components have been installed before the server can be started
function checkInstall() {
if (!fs.existsSync(path.join(__dirname, 'bower_components'))) {
console.log('ERROR: You must run "bower install" before running the Hatch server.');
return false;
}
return true;
}