forked from TechnoX/rcj-rescue-scoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·136 lines (111 loc) · 3.84 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
/**
* Module dependencies.
*
*
* This is where the app is executed and doing some cool stuff, from the
* beginning it spawned sub processes but with the module forever it is not needed.
*
* For the future and for scalability this needs to be implemented correctly with spawning sub processes
* for each processor core on the hosted machine. This version of the server does not support shared memory neither
* does Node.js(87.7% certain). Some alternatives for sharing memory is http://memcached.org or a database containing share variables.
*
* To implement and scale this server you'll need to know what variables needs to be shared and so on (GOOD LUCK MUHAAHAHAHAHAHAHAHAHAHAHAHAHAAHAHAH!).
*
*
* ____Taken from app.js______
* One of the big things to do for scalability is to separate tcp server,static server and application server (api).
*
* Right now they are mixed and running on a single core togeather. To be able to use Node.js to 100% the application
* part of the server should run alone on an own process. And the static files (CSS, HTML and Javascript(frontend)) should
* for performance reasons not run on Node.js but on nginx (http://nginx.org/). One of the fastest static servers out there used
* by many companies. Also it can work as a reverse proxy to support multi Node.js clusters.
*
*/
var cluster = require('cluster')
var logger = require('./config/logger').mainLogger
var env = require('node-env-file')
var numCPUs = require('os').cpus().length;
env('process.env')
/*
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork()
}
cluster.on('exit', function(worker, code, signal) {
// restart process
logger.error('worker ' + worker.process.pid + ' died')
logger.info("Forking new child process")
cluster.fork()
})
}
else {*/
var app = require('./app')
var http = require('http')
var fs = require('fs')
/**
* Get port from environment and store in Express.
*/
// XXX: Is this used anywhere?
var port = parseInt(process.env.WEB_HOSTPORT, 10) || 80
app.set('port', port)
/**
* Create HTTP server.
*/
var server = http.createServer(app)
//https conf
// var options = {
// key: fs.readFileSync('./config/ssl/privkey.pem'),
// cert: fs.readFileSync('./config/ssl/new.cert.cert')
// }
// var https = https.createServer(options,app)
// https.listen(parseInt(process.env.HTTPS_HOSTPORT, 10) || 443)
// https.on('error', onError)
// https.on('listening', onListening)
// socket.io stuff
var io = require('socket.io')(server)
io.on('connection', function (socket) {
socket.on('subscribe', function (data) {
socket.join(data)
logger.debug("Client joined room:" + data)
})
socket.on('unsubscribe', function (data) {
socket.leave(data)
logger.debug("Client detached room:" + data)
})
})
require('./routes/api/lineRuns').connectSocketIo(io)
require('./routes/api/mazeRuns').connectSocketIo(io)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(parseInt(process.env.WEB_HOSTPORT, 10) || 80)
server.on('error', onError)
server.on('listening', onListening)
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error
}
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
logger.error('Port ' + port + ' requires elevated privileges')
process.exit(1)
break
case 'EADDRINUSE':
logger.error('Port ' + port + ' is already in use')
process.exit(1)
break
default:
throw error
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
logger.info('Webserver listening on port ' + server.address().port)
}