2022-01-10 12:54:07 +00:00
|
|
|
/*
|
|
|
|
* Entrypoint for main server script
|
|
|
|
*/
|
2020-01-02 16:58:06 +00:00
|
|
|
|
2020-01-14 16:42:18 +00:00
|
|
|
import url from 'url';
|
2020-01-02 16:58:06 +00:00
|
|
|
import compression from 'compression';
|
|
|
|
import express from 'express';
|
|
|
|
import http from 'http';
|
|
|
|
|
|
|
|
// import baseCss from './components/base.tcss';
|
|
|
|
import forceGC from './core/forceGC';
|
|
|
|
import logger from './core/logger';
|
2020-01-05 16:32:01 +00:00
|
|
|
import rankings from './core/ranking';
|
2020-01-02 16:58:06 +00:00
|
|
|
import models from './data/models';
|
2022-01-10 12:54:07 +00:00
|
|
|
import routes from './routes';
|
2020-11-03 22:43:51 +00:00
|
|
|
import chatProvider from './core/ChatProvider';
|
2020-01-02 16:58:06 +00:00
|
|
|
|
2020-01-14 16:42:18 +00:00
|
|
|
import SocketServer from './socket/SocketServer';
|
|
|
|
import APISocketServer from './socket/APISocketServer';
|
|
|
|
|
2020-01-02 22:36:18 +00:00
|
|
|
|
2022-01-10 12:54:07 +00:00
|
|
|
import { PORT, HOST } from './core/config';
|
|
|
|
import { SECOND } from './core/constants';
|
2020-01-02 16:58:06 +00:00
|
|
|
|
|
|
|
import { startAllCanvasLoops } from './core/tileserver';
|
|
|
|
|
|
|
|
startAllCanvasLoops();
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
|
|
|
|
|
|
|
// Call Garbage Collector every 30 seconds
|
|
|
|
setInterval(forceGC, 15 * 60 * SECOND);
|
|
|
|
|
2020-01-14 16:42:18 +00:00
|
|
|
// create http server
|
2020-01-02 16:58:06 +00:00
|
|
|
const server = http.createServer(app);
|
2020-01-14 16:42:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// websockets
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const usersocket = new SocketServer();
|
|
|
|
const apisocket = new APISocketServer();
|
|
|
|
function wsupgrade(request, socket, head) {
|
|
|
|
const { pathname } = url.parse(request.url);
|
|
|
|
|
|
|
|
if (pathname === '/ws') {
|
|
|
|
usersocket.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
usersocket.wss.emit('connection', ws, request);
|
|
|
|
});
|
|
|
|
} else if (pathname === '/mcws') {
|
|
|
|
apisocket.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
apisocket.wss.emit('connection', ws, request);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 16:58:06 +00:00
|
|
|
server.on('upgrade', wsupgrade);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* use gzip compression for following calls
|
|
|
|
/* level from -1 (default, 6) to 0 (no) from 1 (fastest) to 9 (best)
|
|
|
|
* Set custon filter to make sure that .bmp files get compressed
|
|
|
|
*/
|
2020-01-04 06:00:47 +00:00
|
|
|
app.use(compression({
|
|
|
|
level: 3,
|
2020-01-02 16:58:06 +00:00
|
|
|
filter: (req, res) => {
|
|
|
|
if (res.getHeader('Content-Type') === 'application/octet-stream') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return compression.filter(req, res);
|
2020-01-04 06:00:47 +00:00
|
|
|
},
|
|
|
|
}));
|
2020-01-02 16:58:06 +00:00
|
|
|
|
2022-01-10 12:54:07 +00:00
|
|
|
app.use(routes);
|
2020-01-02 16:58:06 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// ip config
|
|
|
|
// -----------------------------------------------------------------------------
|
2020-11-03 22:43:51 +00:00
|
|
|
// use this if models changed:
|
2020-11-26 18:17:00 +00:00
|
|
|
const promise = models.sync({ alter: { drop: false } })
|
2020-11-03 22:43:51 +00:00
|
|
|
// const promise = models.sync()
|
|
|
|
.catch((err) => logger.error(err.stack));
|
2020-01-02 16:58:06 +00:00
|
|
|
promise.then(() => {
|
2021-07-10 13:51:15 +00:00
|
|
|
rankings.updateRanking();
|
|
|
|
chatProvider.initialize();
|
2021-02-04 23:17:25 +00:00
|
|
|
server.listen(PORT, HOST, () => {
|
2020-01-02 16:58:06 +00:00
|
|
|
const address = server.address();
|
2021-02-04 23:17:25 +00:00
|
|
|
logger.log(
|
2021-02-04 23:29:47 +00:00
|
|
|
'info',
|
2021-02-04 23:17:25 +00:00
|
|
|
`web is running at http://${address.host}:${address.port}/`,
|
|
|
|
);
|
2020-01-02 16:58:06 +00:00
|
|
|
});
|
|
|
|
});
|