Files
blitzkit/src/server.ts
T

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-06-27 01:43:30 +00:00
import express, { Request, Response } from 'express';
2023-06-29 01:34:02 +00:00
import normalizeRouteReturnable from './core/express/normalizeRouteReturnable.js';
2023-06-29 01:43:59 +00:00
import withAutoRefresh from './core/express/withAutoRefresh.js';
2023-06-27 01:43:30 +00:00
import isDev from './core/node/isDev.js';
import { Registry } from './events/interactionCreate/index.js';
2023-07-05 01:16:19 +00:00
import { statsRoute } from './routes/stats.js';
2023-06-27 01:43:30 +00:00
2023-06-29 01:34:02 +00:00
export type RouteReturnable = JSX.Element | Promise<JSX.Element>;
2023-06-27 01:43:30 +00:00
2023-06-29 01:34:02 +00:00
export interface RouteRegistry<HandlesInteraction extends boolean = false>
2023-06-27 01:43:30 +00:00
extends Registry {
route: string;
handlesInteraction?: HandlesInteraction;
handler: (
req: Request,
res: Response,
2023-06-29 01:34:02 +00:00
) => HandlesInteraction extends true ? void : RouteReturnable;
2023-06-27 01:43:30 +00:00
}
2023-06-26 15:05:05 +00:00
const app = express();
2023-07-05 01:16:19 +00:00
([statsRoute] as RouteRegistry[]).forEach((registry) => {
2023-06-27 01:43:30 +00:00
if (!(isDev() ? registry.inDevelopment : registry.inProduction)) return;
2023-06-26 17:25:28 +00:00
2023-06-27 01:43:30 +00:00
app.get(registry.route, async (req, res) => {
2023-06-29 01:43:59 +00:00
console.log(req.originalUrl);
2023-06-27 01:43:30 +00:00
2023-06-29 01:43:59 +00:00
const returnable = registry.handler(req, res);
2023-06-27 01:43:30 +00:00
if (registry.handlesInteraction) return;
2023-06-29 01:43:59 +00:00
res.send(
withAutoRefresh(await normalizeRouteReturnable(returnable), 60 * 1000),
);
2023-06-26 17:25:28 +00:00
});
2023-06-26 15:05:05 +00:00
});
2023-06-26 17:25:28 +00:00
app.listen(process.env.PORT ?? 3000);