/** * 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 url = new URL(c.req.url); const targetUrl = `${ENCRYPTID_URL}${url.pathname}${url.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 (url.pathname.endsWith("/count")) return c.json({ unreadCount: 0 }); if (url.pathname === "/api/notifications") return c.json({ notifications: [] }); return c.json({ error: "Notification service unavailable" }, 503); } });