2020-01-02 16:58:06 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2020-01-14 16:42:18 +00:00
|
|
|
import url from 'url';
|
2020-01-02 16:58:06 +00:00
|
|
|
import path from 'path';
|
|
|
|
import compression from 'compression';
|
|
|
|
import express from 'express';
|
|
|
|
import http from 'http';
|
|
|
|
import etag from 'etag';
|
|
|
|
|
|
|
|
|
|
|
|
// import baseCss from './components/base.tcss';
|
|
|
|
import forceGC from './core/forceGC';
|
|
|
|
import assets from './assets.json'; // eslint-disable-line import/no-unresolved
|
|
|
|
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';
|
2020-11-03 22:43:51 +00:00
|
|
|
import chatProvider from './core/ChatProvider';
|
2021-02-02 20:07:26 +00:00
|
|
|
import { expressTTag } from './core/ttag';
|
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 16:58:06 +00:00
|
|
|
import {
|
|
|
|
api,
|
|
|
|
tiles,
|
|
|
|
chunks,
|
|
|
|
admintools,
|
|
|
|
resetPassword,
|
|
|
|
} from './routes';
|
2021-01-29 01:43:03 +00:00
|
|
|
import generateGlobePage from './ssr-components/Globe';
|
|
|
|
import generateMainPage from './ssr-components/Main';
|
2020-01-02 22:36:18 +00:00
|
|
|
|
2020-01-02 16:58:06 +00:00
|
|
|
import { SECOND, MONTH } from './core/constants';
|
2021-02-04 23:17:25 +00:00
|
|
|
import { PORT, HOST, GUILDED_INVITE } from './core/config';
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// API
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
app.use('/api', api);
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Serving Zoomed Tiless
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
app.use('/tiles', tiles);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// public folder
|
|
|
|
// (this should be served with nginx or other webserver)
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
app.use(express.static(path.join(__dirname, 'public'), {
|
|
|
|
maxAge: 3 * MONTH,
|
|
|
|
extensions: ['html'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2021-02-04 23:17:25 +00:00
|
|
|
// Redirecct to guilded
|
2020-01-02 16:58:06 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
2020-06-23 06:21:56 +00:00
|
|
|
app.use('/guilded', (req, res) => {
|
|
|
|
res.redirect(GUILDED_INVITE);
|
|
|
|
});
|
2020-01-02 16:58:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Serving Chunks
|
|
|
|
// -----------------------------------------------------------------------------
|
2020-01-27 23:24:25 +00:00
|
|
|
app.get('/chunks/:c([0-9]+)/:x([0-9]+)/:y([0-9]+)(/)?:z([0-9]+)?.bmp', chunks);
|
2020-01-02 16:58:06 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Admintools
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
app.use('/admintools', admintools);
|
|
|
|
|
2021-02-02 20:07:26 +00:00
|
|
|
/*
|
|
|
|
* decide which language to use
|
|
|
|
*/
|
|
|
|
app.use(expressTTag);
|
|
|
|
|
2020-01-02 22:36:18 +00:00
|
|
|
|
2020-01-02 16:58:06 +00:00
|
|
|
//
|
|
|
|
// Password Reset Link
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
app.use('/reset_password', resetPassword);
|
|
|
|
|
|
|
|
|
2020-01-02 22:36:18 +00:00
|
|
|
//
|
2020-01-03 02:21:57 +00:00
|
|
|
// 3D Globe (react generated)
|
2020-01-02 22:36:18 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const globeEtag = etag(
|
2021-01-29 01:15:33 +00:00
|
|
|
assets.globe.js.join('_'),
|
2020-01-02 22:36:18 +00:00
|
|
|
{ weak: true },
|
|
|
|
);
|
|
|
|
app.get('/globe', async (req, res) => {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': `private, max-age=${15 * 60}`, // seconds
|
|
|
|
'Content-Type': 'text/html; charset=utf-8',
|
2020-01-03 03:12:21 +00:00
|
|
|
ETag: globeEtag,
|
2020-01-02 22:36:18 +00:00
|
|
|
});
|
|
|
|
|
2020-01-03 02:21:57 +00:00
|
|
|
if (req.headers['if-none-match'] === globeEtag) {
|
2020-01-02 22:36:18 +00:00
|
|
|
res.status(304).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:07:26 +00:00
|
|
|
res.status(200).send(generateGlobePage(req.lang));
|
2020-01-02 22:36:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-01-02 16:58:06 +00:00
|
|
|
//
|
2020-01-03 02:21:57 +00:00
|
|
|
// Main Page (react generated)
|
2020-01-02 16:58:06 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const indexEtag = etag(
|
2021-01-29 01:15:33 +00:00
|
|
|
assets.client.js.join('_'),
|
2020-01-02 16:58:06 +00:00
|
|
|
{ weak: true },
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': `private, max-age=${15 * 60}`, // seconds
|
2020-01-02 22:36:18 +00:00
|
|
|
'Content-Type': 'text/html; charset=utf-8',
|
2020-01-02 16:58:06 +00:00
|
|
|
ETag: indexEtag,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (req.headers['if-none-match'] === indexEtag) {
|
|
|
|
res.status(304).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-18 13:51:34 +00:00
|
|
|
res.status(200).send(generateMainPage(req.lang));
|
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
|
|
|
});
|
|
|
|
});
|