Forbid castling when king doesn't move and is under check
[vchess.git] / server / bin / www
CommitLineData
1d184b4c
BA
1#!/usr/bin/env node
2
3/**
4 * Module dependencies.
5 */
6
7var app = require('../app');
8var debug = require('debug')('vc:server');
9var http = require('http');
10
11/**
12 * Get port from environment and store in Express.
13 */
14
15var port = normalizePort(process.env.PORT || '3000');
16app.set('port', port);
17
18/**
19 * Create HTTP server.
20 */
21
22var server = http.createServer(app);
23
d431028c
BA
24/*
25 * CRON tasks
26 */
27
28var cron = require('node-cron');
29var UserModel = require("../models/User");
30var ChallengeModel = require("../models/Challenge");
31var GameModel = require("../models/Game");
32cron.schedule('0 0 0 * * *', function() {
dac39588
BA
33 // Remove some old users, challenges and games every 24h
34 UserModel.cleanUsersDb();
35 ChallengeModel.removeOld();
36 GameModel.cleanGamesDb();
d431028c
BA
37});
38
1d184b4c
BA
39/**
40 * Listen on provided port, on all network interfaces.
41 */
42
43server.listen(port);
44server.on('error', onError);
45server.on('listening', onListening);
46
47/*
48 * Sockets handling
49 */
50
51const WebSocket = require('ws');
52const wss = new WebSocket.Server({ server: server });
53require('../sockets')(wss);
54
55/**
56 * Normalize a port into a number, string, or false.
57 */
58
59function 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
79function 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
107function 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}