forked from ClickHouse/librechat-admin-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
75 lines (62 loc) · 2.9 KB
/
server.ts
File metadata and controls
75 lines (62 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Glob } from 'bun';
import { join } from 'node:path';
import { metricsResponse, httpRequestsTotal, httpRequestDurationSeconds } from './src/server/metrics';
const CLIENT_DIR = join(import.meta.dir, 'dist', 'client');
const SERVER_ENTRY = new URL('./dist/server/server.js', import.meta.url);
const env = process.env;
const ONE_DAY = 86400;
const rawMaxAge = Number(env.ADMIN_PANEL_STATIC_CACHE_MAX_AGE ?? env.STATIC_CACHE_MAX_AGE);
const rawSMaxAge = Number(env.ADMIN_PANEL_STATIC_CACHE_S_MAX_AGE ?? env.STATIC_CACHE_S_MAX_AGE);
const maxAge = Number.isNaN(rawMaxAge) ? ONE_DAY * 2 : rawMaxAge;
const sMaxAge = Number.isNaN(rawSMaxAge) ? ONE_DAY : rawSMaxAge;
const NO_CACHE: Record<string, string> = {
'Cache-Control': env.ADMIN_PANEL_INDEX_CACHE_CONTROL ?? env.INDEX_CACHE_CONTROL ?? 'no-cache, no-store, must-revalidate',
Pragma: env.ADMIN_PANEL_INDEX_PRAGMA ?? env.INDEX_PRAGMA ?? 'no-cache',
Expires: env.ADMIN_PANEL_INDEX_EXPIRES ?? env.INDEX_EXPIRES ?? '0',
};
const LONG_CACHE: Record<string, string> = {
'Cache-Control': `public, max-age=${maxAge}, s-maxage=${sMaxAge}`,
};
const NEVER_CACHE = new Set(['manifest.json', 'sw.js', 'robots.txt']);
function getCacheHeaders(filePath: string): Record<string, string> {
const fileName = filePath.split('/').pop() ?? '';
if (NEVER_CACHE.has(fileName)) return NO_CACHE;
if (filePath.startsWith('assets/')) return LONG_CACHE;
return {};
}
type Handler = { default: { fetch: (req: Request) => Promise<Response> } };
const { default: handler } = (await import(SERVER_ENTRY.href)) as Handler;
if (!process.env.ADMIN_PANEL_METRICS_SECRET) {
console.warn('[metrics] ADMIN_PANEL_METRICS_SECRET is not set — /metrics will return 401 for all requests');
}
async function buildStaticRoutes(): Promise<Record<string, () => Response>> {
const routes: Record<string, () => Response> = {};
for await (const path of new Glob('**/*').scan(CLIENT_DIR)) {
const file = Bun.file(`${CLIENT_DIR}/${path}`);
const cache = getCacheHeaders(path);
routes[`/${path}`] = () =>
new Response(file, { headers: { 'Content-Type': file.type, ...cache } });
}
return routes;
}
Bun.serve({
port: Number(process.env.PORT ?? 3000),
routes: {
...(await buildStaticRoutes()),
'/metrics': (req) => metricsResponse(req),
'/*': async (req) => {
const url = new URL(req.url);
const end = httpRequestDurationSeconds.startTimer({ method: req.method, path: url.pathname });
const res = await handler.fetch(req);
const statusCode = String(res.status);
httpRequestsTotal.inc({ method: req.method, path: url.pathname, status_code: statusCode });
end({ status_code: statusCode });
const patched = new Response(res.body, res);
for (const [k, v] of Object.entries(NO_CACHE)) {
patched.headers.set(k, v);
}
return patched;
},
},
});
console.log(`Admin panel listening on http://localhost:${process.env.PORT ?? 3000}`);