Files
blitzkit/src/server.ts
T

40 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-27 17:44:15 +00:00
import normalizeRouterReturnable from './core/express/normalizeRouterReturnable.js';
2023-06-27 01:43:30 +00:00
import isDev from './core/node/isDev.js';
import { Registry } from './events/interactionCreate/index.js';
2023-06-27 17:44:15 +00:00
import stats from './routers/stats.js';
2023-06-27 01:43:30 +00:00
2023-06-27 17:44:15 +00:00
export type RouterReturnable = JSX.Element | Promise<JSX.Element>;
2023-06-27 01:43:30 +00:00
2023-06-27 17:44:15 +00:00
export interface RouterRegistry<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-27 17:44:15 +00:00
) => HandlesInteraction extends true ? void : RouterReturnable;
2023-06-27 01:43:30 +00:00
}
2023-06-26 15:05:05 +00:00
const app = express();
2023-06-27 17:44:15 +00:00
([stats] as RouterRegistry[]).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) => {
const returnable = registry.handler(req, res);
if (registry.handlesInteraction) return;
2023-06-27 17:44:15 +00:00
const response = await normalizeRouterReturnable(returnable);
2023-06-27 01:43:30 +00:00
res.writeHead(200, {
'Content-Type': 'image/svg+xml',
'Content-Length': response.length,
});
res.end(response);
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);