Forbid castling when king doesn't move and is under check
[vchess.git] / server / bin / www
1 #!/usr/bin/env node
2
3 /**
4 * Module dependencies.
5 */
6
7 var app = require('../app');
8 var debug = require('debug')('vc:server');
9 var http = require('http');
10
11 /**
12 * Get port from environment and store in Express.
13 */
14
15 var port = normalizePort(process.env.PORT || '3000');
16 app.set('port', port);
17
18 /**
19 * Create HTTP server.
20 */
21
22 var server = http.createServer(app);
23
24 /*
25 * CRON tasks
26 */
27
28 var cron = require('node-cron');
29 var UserModel = require("../models/User");
30 var ChallengeModel = require("../models/Challenge");
31 var GameModel = require("../models/Game");
32 cron.schedule('0 0 0 * * *', function() {
33 // Remove some old users, challenges and games every 24h
34 UserModel.cleanUsersDb();
35 ChallengeModel.removeOld();
36 GameModel.cleanGamesDb();
37 });
38
39 /**
40 * Listen on provided port, on all network interfaces.
41 */
42
43 server.listen(port);
44 server.on('error', onError);
45 server.on('listening', onListening);
46
47 /*
48 * Sockets handling
49 */
50
51 const WebSocket = require('ws');
52 const wss = new WebSocket.Server({ server: server });
53 require('../sockets')(wss);
54
55 /**
56 * Normalize a port into a number, string, or false.
57 */
58
59 function normalizePort(val) {
60 var port = parseInt(val, 10);
61
62 if (isNaN(port)) {
63 // named pipe
64 return val;
65 }
66
67 if (port >= 0) {
68 // port number
69 return port;
70 }
71
72 return false;
73 }
74
75 /**
76 * Event listener for HTTP server "error" event.
77 */
78
79 function onError(error) {
80 if (error.syscall !== 'listen') {
81 throw error;
82 }
83
84 var bind = typeof port === 'string'
85 ? 'Pipe ' + port
86 : 'Port ' + port;
87
88 // handle specific listen errors with friendly messages
89 switch (error.code) {
90 case 'EACCES':
91 console.error(bind + ' requires elevated privileges');
92 process.exit(1);
93 break;
94 case 'EADDRINUSE':
95 console.error(bind + ' is already in use');
96 process.exit(1);
97 break;
98 default:
99 throw error;
100 }
101 }
102
103 /**
104 * Event listener for HTTP server "listening" event.
105 */
106
107 function onListening() {
108 var addr = server.address();
109 var bind = typeof addr === 'string'
110 ? 'pipe ' + addr
111 : 'port ' + addr.port;
112 debug('Listening on ' + bind);
113 }