import { recoveryApp } from "./recovery.ts"; import { Hono } from "jsr:@hono/hono@4"; import { MetadataService } from "jsr:@simplewebauthn/server@13"; import { initDb } from "./db.ts"; import { pingValkey } from "./valkey.ts"; import { uiApp } from "../ui/mod.ts"; import { passRoutes } from "./routes/passes_magic.ts"; import { eventRoutes } from "./routes/events.ts"; import { sessionRoutes } from "./routes/sessions.ts"; import { forwardAuthRoutes } from "./routes/auth_forward.ts"; import { authRoutes } from "./routes/auth.ts"; import { adminRoutes } from "./routes/admin.ts"; import { startConnectRpcServer } from "./rpc.ts"; type Variables = { userId: string; }; export const app: Hono<{ Variables: Variables }> = new Hono< { Variables: Variables } >(); // Middleware app.use("*", async (_c, next) => { // TODO: Add mTLS verification or internal network constraints await next(); }); // Mount Sub-routers app.route("/pass", passRoutes); app.route("/", eventRoutes); app.route("/", sessionRoutes); app.route("/", forwardAuthRoutes); app.route("/api/admin", adminRoutes); app.route("/", authRoutes); app.route("/api/recovery", recoveryApp); // Initialize RPC Services startConnectRpcServer(app as any); // Type cast due to Hono version mismatch internally, though here it's compatible // Mount UI Routes app.route("/", uiApp); if (import.meta.main) { const rpID = Deno.env.get("RP_ID"); const origin = Deno.env.get("ORIGIN"); if (!rpID || !origin) { throw new Error( "Missing critical environment variables: RP_ID and ORIGIN must be set.", ); } console.log("[Auth API] Initializing FIDO MDS3 Metadata Blob..."); try { // SIDE EFFECT: Fetch MDS3 metadata dynamically on startup await MetadataService.initialize(); console.log("[Auth API] FIDO MDS3 Metadata Blob successfully loaded."); } catch (error) { console.error( "[Auth API] Fatal Error: Failed to initialize FIDO MDS3 Metadata Blob:", ); console.error(error); Deno.exit(1); } try { // SIDE EFFECT: Initialize database schema on startup await initDb(); } catch (error) { console.error( "[Auth API] Fatal Error: Failed to initialize Database Schema:", error, ); console.error(error); Deno.exit(1); } const PORT = parseInt(Deno.env.get("PORT") || "8000"); // Fail fast if connection to Valkey fails during startup await pingValkey(); console.log(`Auth API Server running on port ${PORT}`); Deno.serve({ port: PORT }, app.fetch); }