47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { Elysia } from "elysia";
|
|
import { ingest } from "./routes/pings";
|
|
import { monitors } from "./routes/monitors";
|
|
import { account } from "./routes/auth";
|
|
import { internal } from "./routes/internal";
|
|
import { migrate } from "./db";
|
|
import { SECURITY_HEADERS } from "../../shared/auth";
|
|
|
|
await migrate();
|
|
|
|
const elysia = new Elysia()
|
|
.get("/", () => ({
|
|
name: "PingQL API",
|
|
version: "1",
|
|
docs: "https://pingql.com/docs",
|
|
}))
|
|
.use(account)
|
|
.use(monitors)
|
|
.use(ingest)
|
|
.use(internal);
|
|
|
|
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}`);
|