Files
patriothacks/api/_lib/devServer.ts
T

41 lines
1.3 KiB
TypeScript

import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import postBlob from "../blob/index.ts";
import getBlob from "../blob/[id].ts";
import postSync from "../sync/index.ts";
import getAggregate from "../admin/aggregate.ts";
import { sendJson } from "./httpUtils.ts";
export function createApiServer() {
return createServer(async (req: IncomingMessage, res: ServerResponse) => {
const pathname = new URL(req.url ?? "/", "http://localhost").pathname;
try {
if (pathname === "/api/sync") {
await postSync(req, res);
return;
}
if (pathname === "/api/admin/aggregate") {
await getAggregate(req, res);
return;
}
if (pathname === "/api/blob") {
await postBlob(req, res);
return;
}
if (pathname.startsWith("/api/blob/")) {
await getBlob(req, res);
return;
}
sendJson(res, 404, { error: "not found" });
} catch (err) {
sendJson(res, 500, { error: "internal error", detail: (err as Error).message });
}
});
}
if (import.meta.url === `file://${process.argv[1]}`) {
const port = Number(process.env.PORT ?? 3001);
createApiServer().listen(port, () => {
console.log(`blob API dev server listening on http://localhost:${port}`);
});
}