add history api

This commit is contained in:
HF 2020-01-09 18:54:59 +01:00
parent 060540d23d
commit 8be7d8ce41
4 changed files with 46 additions and 2 deletions

View File

@ -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(<Html {...htmldata} />);

View File

@ -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;

35
src/routes/api/history.js Normal file
View File

@ -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;

View File

@ -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
*/