test: huh?

This commit is contained in:
nate 2026-03-26 12:29:12 +04:00
parent e34f1938f7
commit 9e1ba85043
1 changed files with 28 additions and 19 deletions

View File

@ -15,22 +15,7 @@ const SECURITY_HEADERS = {
"Referrer-Policy": "strict-origin-when-cross-origin", "Referrer-Policy": "strict-origin-when-cross-origin",
}; };
const CORS_HEADERS: Record<string, string> = { const elysia = new Elysia()
"access-control-allow-credentials": "true",
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS",
"access-control-allow-headers": "Content-Type, Authorization",
};
const app = new Elysia()
.onRequest(({ request, set }) => {
const origin = request.headers.get("origin") || "*";
set.headers["access-control-allow-origin"] = origin;
Object.assign(set.headers, CORS_HEADERS, SECURITY_HEADERS);
if (request.method === "OPTIONS") {
set.status = 204;
return new Response(null, { status: 204, headers: { ...CORS_HEADERS, ...SECURITY_HEADERS, "access-control-allow-origin": origin } });
}
})
.get("/", () => ({ .get("/", () => ({
name: "PingQL API", name: "PingQL API",
version: "1", version: "1",
@ -39,7 +24,31 @@ const app = new Elysia()
.use(account) .use(account)
.use(monitors) .use(monitors)
.use(ingest) .use(ingest)
.use(internal) .use(internal);
.listen(3001);
console.log(`PingQL API running at http://localhost:${app.server?.port}`); // Wrap Elysia with Bun.serve to guarantee CORS + security headers on every response
const server = Bun.serve({
port: 3001,
async fetch(req) {
const origin = req.headers.get("origin") || "*";
const corsHeaders: Record<string, string> = {
"access-control-allow-origin": origin,
"access-control-allow-credentials": "true",
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS",
"access-control-allow-headers": "Content-Type, Authorization",
...SECURITY_HEADERS,
};
if (req.method === "OPTIONS") {
return new Response(null, { status: 204, headers: corsHeaders });
}
const res = await elysia.handle(req);
for (const [k, v] of Object.entries(corsHeaders)) {
res.headers.set(k, v);
}
return res;
},
});
console.log(`PingQL API running at http://localhost:${server.port}`);