diff --git a/src/components/Main.jsx b/src/components/Main.jsx index 6062533..897fb90 100644 --- a/src/components/Main.jsx +++ b/src/components/Main.jsx @@ -10,7 +10,7 @@ import ReactDOM from 'react-dom/server'; import Html from './Html'; import assets from './assets.json'; -import { ASSET_SERVER } from '../core/config'; +import { ASSET_SERVER, BACKUP_URL } from '../core/config'; const data = { title: 'PixelPlanet.fun', @@ -35,7 +35,10 @@ const data = { */ function generateMainPage(countryCoords: Cell): string { const [x, y] = countryCoords; - const code = `window.coordx=${x};window.coordy=${y};window.assetserver="${ASSET_SERVER}";`; + let code = `window.coordx=${x};window.coordy=${y};window.assetserver="${ASSET_SERVER}";`; + if (BACKUP_URL) { + code += `window.backupurl="${BACKUP_URL}";`; + } const htmldata = { ...data, code }; const html = ReactDOM.renderToStaticMarkup(); diff --git a/src/core/config.js b/src/core/config.js index 7dfaef9..0f7f7a0 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -16,6 +16,9 @@ export const ASSET_SERVER = process.env.ASSET_SERVER || '.'; export const USE_XREALIP = process.env.USE_XREALIP || false; +export const BACKUP_URL = process.env.BACKUP_URL || null; +export const BACKUP_DIR = process.env.BACKUP_DIR || null; + // Proxycheck export const USE_PROXYCHECK = parseInt(process.env.USE_PROXYCHECK, 10) || false; diff --git a/src/routes/api/history.js b/src/routes/api/history.js new file mode 100644 index 0000000..2e5ae8c --- /dev/null +++ b/src/routes/api/history.js @@ -0,0 +1,35 @@ +/** + * + * @flow + */ + +import fs from 'fs'; +import type { Request, Response } from 'express'; + +import { BACKUP_DIR } from '../../core/config'; + +async function history(req: Request, res: Response) { + const { day, id } = req.query; + if (!BACKUP_DIR || !day || !id || day.includes('/') || day.includes('\\')) { + res.status(404).end(); + } + const path = `${BACKUP_DIR}/${day}/${id}`; + + try { + if (!fs.existsSync(path)) { + res.status(404).end(); + } + + const dirs = fs.readdirSync(path) + const filteredDir = dirs.filter(item => item !== 'tiles') + res.set({ + 'Cache-Control': `public, max-age=${60 * 60}`, // seconds + }); + res.json(filteredDir); + + } catch { + res.status(404).end(); + } +} + +export default history; diff --git a/src/routes/api/index.js b/src/routes/api/index.js index ffcd9d7..85fdd47 100644 --- a/src/routes/api/index.js +++ b/src/routes/api/index.js @@ -23,6 +23,7 @@ import mctp from './mctp'; import pixel from './pixel'; import auth from './auth'; import ranking from './ranking'; +import history from './history'; const router = express.Router(); @@ -30,6 +31,8 @@ const router = express.Router(); // this route doesn't need passport router.get('/ranking', ranking); +router.get('/history', history); + /* * get user session */