Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import { 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 { canonicalHandlePlugin } from "./canonical-handle-plugin.ts";import { getDb } from "./db/index.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 { 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 and prod auth must not coexist: the dev-session fallback trusts a `did=`// cookie without a password check. A prod instance that also sets DEV_ACCOUNTS// would still ignore it (getDevAccounts returns null when OAuth env is set),// but bail loudly to flag misconfiguration rather than silently dropping it.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).", );}
export function buildApp() { getDb(); return new Elysia() .onError(({ error, code, request }) => { const renderNotFound = ( key: keyof ReturnType<typeof t>["error"] | null, fallback: string, vars?: Record<string, string> | 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" }, }); };
if (error instanceof AppError) { if (error.statusCode === 404) { return renderNotFound( error.i18nKey ?? "routeNotFound", error.message, error.i18nVars, ); } return new Response(error.message, { status: error.statusCode }); } if (code === "NOT_FOUND") { return renderNotFound("routeNotFound", "Not found"); } console.error("Unhandled error:", error); return new Response("Internal server error", { status: 500 }); }) .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(exploreRoutes) .use(searchRoutes) .use(membershipRoutes) .use(noteRoutes) .use(wikiCreationRoutes) .use(wikiRoutes);}