40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
/**
|
|
* Notification REST API — mounted at /api/notifications
|
|
*
|
|
* Proxies all requests to the encryptid service which has direct DB access.
|
|
* The rspace container cannot reach the encryptid database directly.
|
|
*/
|
|
|
|
import { Hono } from "hono";
|
|
|
|
const ENCRYPTID_URL = process.env.ENCRYPTID_INTERNAL_URL || "http://encryptid:3000";
|
|
|
|
export const notificationRouter = new Hono();
|
|
|
|
// Proxy all notification requests to encryptid
|
|
notificationRouter.all("/*", async (c) => {
|
|
const path = `/api/notifications${c.req.path === "/" ? "" : c.req.path}`;
|
|
const search = new URL(c.req.url).search;
|
|
const targetUrl = `${ENCRYPTID_URL}${path}${search}`;
|
|
|
|
const headers = new Headers(c.req.raw.headers);
|
|
headers.delete("host");
|
|
|
|
try {
|
|
const res = await fetch(targetUrl, {
|
|
method: c.req.method,
|
|
headers,
|
|
body: c.req.method !== "GET" && c.req.method !== "HEAD" ? c.req.raw.body : undefined,
|
|
// @ts-ignore duplex needed for streaming request bodies
|
|
duplex: "half",
|
|
});
|
|
return new Response(res.body, { status: res.status, headers: res.headers });
|
|
} catch (e: any) {
|
|
console.error("[notifications proxy] Failed to reach encryptid:", e?.message);
|
|
// Return safe fallbacks instead of hanging
|
|
if (c.req.path === "/count") return c.json({ unreadCount: 0 });
|
|
if (c.req.path === "/") return c.json({ notifications: [] });
|
|
return c.json({ error: "Notification service unavailable" }, 503);
|
|
}
|
|
});
|