From 90d4b76b31c55546fb9ef620a5ddb903daf0711d Mon Sep 17 00:00:00 2001 From: tresabhi Date: Thu, 29 Jun 2023 01:43:59 +0000 Subject: [PATCH] feat: added auto refresh --- src/core/express/withAutoRefresh.ts | 23 +++++++++++++++++++++++ src/routes/stats.tsx | 2 +- src/server.ts | 14 ++++++-------- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 src/core/express/withAutoRefresh.ts diff --git a/src/core/express/withAutoRefresh.ts b/src/core/express/withAutoRefresh.ts new file mode 100644 index 000000000..e29b780cf --- /dev/null +++ b/src/core/express/withAutoRefresh.ts @@ -0,0 +1,23 @@ +export default function withAutoRefresh(svg: string, refreshAfter: number) { + return ` + + + + + + + + + ${svg} + + + `; +} diff --git a/src/routes/stats.tsx b/src/routes/stats.tsx index 3ce638a67..ccc329a80 100644 --- a/src/routes/stats.tsx +++ b/src/routes/stats.tsx @@ -6,7 +6,7 @@ import { RouteRegistry } from '../server.js'; export default { route: '/stats/:type/:period', inProduction: true, - inDevelopment: false, + inDevelopment: true, async handler(req) { const type = req.params.type as StatType; diff --git a/src/server.ts b/src/server.ts index 8048bb0fb..660710863 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,5 +1,6 @@ import express, { Request, Response } from 'express'; import normalizeRouteReturnable from './core/express/normalizeRouteReturnable.js'; +import withAutoRefresh from './core/express/withAutoRefresh.js'; import isDev from './core/node/isDev.js'; import { Registry } from './events/interactionCreate/index.js'; import stats from './routes/stats.js'; @@ -23,17 +24,14 @@ const app = express(); if (!(isDev() ? registry.inDevelopment : registry.inProduction)) return; app.get(registry.route, async (req, res) => { - const returnable = registry.handler(req, res); + console.log(req.originalUrl); + const returnable = registry.handler(req, res); if (registry.handlesInteraction) return; - const response = await normalizeRouteReturnable(returnable); - - res.writeHead(200, { - 'Content-Type': 'image/svg+xml', - 'Content-Length': response.length, - }); - res.end(response); + res.send( + withAutoRefresh(await normalizeRouteReturnable(returnable), 60 * 1000), + ); }); });