import { staticPlugin } from "@elysiajs/static"; import { Elysia } from "elysia"; import { getAtprotoEnv } from "../atproto/env.ts"; import { atprotoRoutes } from "../atproto/routes.ts"; import { AppError } from "../lib/errors.ts"; import { fmt, resolveLocale, t } from "../lib/i18n/index.ts"; import { notFoundPage } from "../views/not-found.ts"; import { removedPage } from "../views/removed.ts"; import { canonicalHandlePlugin } from "./canonical-handle-plugin.ts"; import { getDb } from "./db/index.ts"; import { pruneExpiredAppSessions } from "./db/queries/index.ts"; import { atUriRoutes } from "./routes/at-uri.ts"; import { blobRoutes } from "./routes/blob.ts"; import { bookmarkRoutes } from "./routes/bookmark.ts"; import { collabRoutes } from "./routes/collab.ts"; import { exploreRoutes } from "./routes/explore.ts"; import { homeRoute } from "./routes/home.ts"; import { localeRoutes } from "./routes/locale.ts"; import { membershipRoutes } from "./routes/membership.ts"; import { noteRoutes } from "./routes/note.ts"; import { ogRoutes } from "./routes/og.ts"; import { profileRoutes } from "./routes/profile.ts"; import { pwaRoutes } from "./routes/pwa.ts"; import { searchRoutes } from "./routes/search.ts"; import { seoRoutes } from "./routes/seo.ts"; import { themeRoutes } from "./routes/theme.ts"; import { wikiCreationRoutes, wikiRoutes } from "./routes/wiki.ts"; // Dev/prod auth must not coexist: the dev fallback trusts a `did=` cookie with no password check. if (getAtprotoEnv() !== null && process.env["DEV_ACCOUNTS"]) { throw new Error( "Dev and prod auth are mutually exclusive — unset DEV_ACCOUNTS or the OAuth env vars (PUBLIC_URL / OAUTH_PRIVATE_KEY_PATH).", ); } // An error page reflects who asked and what they may see; nothing in front of // us may store one for the next visitor. const PRIVATE_CACHE = "private, no-store"; export function buildApp() { getDb(); pruneExpiredAppSessions(); return new Elysia() .onError(({ error, code, request }) => { const renderNotFound = ( key: keyof ReturnType["error"] | null, fallback: string, vars?: Record | null, ) => { const locale = resolveLocale( request.headers.get("cookie"), request.headers.get("accept-language"), ); const msg = t(locale); const template = key ? msg.error[key] : fallback; const body = vars ? fmt(template, vars) : template; return new Response(notFoundPage(body, { locale }), { status: 404, headers: { "content-type": "text/html; charset=utf-8", "cache-control": PRIVATE_CACHE, }, }); }; if (error instanceof AppError) { if (error.statusCode === 404) { return renderNotFound( error.i18nKey ?? "routeNotFound", error.message, error.i18nVars, ); } if (error.statusCode === 410) { const locale = resolveLocale( request.headers.get("cookie"), request.headers.get("accept-language"), ); return new Response(removedPage({ locale }), { status: 410, headers: { "content-type": "text/html; charset=utf-8", "cache-control": PRIVATE_CACHE, }, }); } return new Response(error.message, { status: error.statusCode, headers: { "cache-control": PRIVATE_CACHE }, }); } if (code === "NOT_FOUND") { return renderNotFound("routeNotFound", "Not found"); } console.error("Unhandled error:", error); return new Response("Internal server error", { status: 500, headers: { "cache-control": PRIVATE_CACHE }, }); }) .use(staticPlugin({ prefix: "/public", assets: "public" })) .use(canonicalHandlePlugin) .use(atprotoRoutes()) .use(collabRoutes) .use(blobRoutes) .use(bookmarkRoutes) .use(localeRoutes) .use(themeRoutes) .use(profileRoutes) .use(ogRoutes) .use(homeRoute) .use(seoRoutes) .use(atUriRoutes) .use(pwaRoutes) .use(exploreRoutes) .use(searchRoutes) .use(membershipRoutes) .use(noteRoutes) .use(wikiCreationRoutes) .use(wikiRoutes); }