/// import { allRoutes } from "./routes.ts"; import { serveDir } from "@std/http/file-server"; const PORT = parseInt(Deno.env.get("PORT") || "3001"); const VITE_DEV_SERVER = "http://localhost:5173"; // Default Vite dev server port // Check if we're in development mode (Vite dev server running) const isDevelopment = Deno.env.get("NODE_ENV") !== "production"; async function defaultHandler(req: Request): Promise { const url = new URL(req.url); // In development, proxy to Vite dev server if (isDevelopment) { try { // For SPA routing: All non-API routes go to Vite // Vite will handle returning index.html for unknown routes const viteUrl = new URL(url.pathname + url.search, VITE_DEV_SERVER); // Create a clean headers object without Host header const proxyHeaders = new Headers(); for (const [key, value] of req.headers.entries()) { // Skip host and connection headers if ( key.toLowerCase() !== "host" && key.toLowerCase() !== "connection" ) { proxyHeaders.set(key, value); } } return await fetch(viteUrl.toString(), { method: req.method, headers: proxyHeaders, body: req.method !== "GET" && req.method !== "HEAD" ? req.body : undefined, }); } catch (_error) { return new Response( ` Vite Dev Server Not Running

⚠️ Vite Dev Server Not Running

The Deno server is running, but it can't connect to the Vite dev server at ${VITE_DEV_SERVER}.

Start the Vite dev server in a separate terminal:

cd frontend-v2
deno task dev:vite

Then refresh this page.

`, { status: 503, headers: { "Content-Type": "text/html" }, } ); } } else { // In production, serve static files from dist/ // For SPA routing: return index.html for non-file requests const response = await serveDir(req, { fsRoot: "./dist", quiet: true, }); // If file not found (404), return index.html for SPA routing if (response.status === 404) { const indexFile = await Deno.readFile("./dist/index.html"); return new Response(indexFile, { headers: { "Content-Type": "text/html" }, }); } return response; } } // Custom routing handler that directly matches routes async function handler(req: Request): Promise { const url = new URL(req.url); // Only log non-asset requests to reduce noise if ( !url.pathname.startsWith("/src/") && !url.pathname.startsWith("/@") && !url.pathname.includes(".js") && !url.pathname.includes(".css") ) { console.log(`[${req.method}] ${url.pathname}`); } // Try to match against our defined routes first for (const routeDef of allRoutes) { const match = routeDef.pattern.exec(url); if (match && req.method === routeDef.method) { console.log(`✓ Matched API route: ${routeDef.method} ${url.pathname}`); return await routeDef.handler(req); } } // Fall back to default handler (proxy to Vite or serve static) return await defaultHandler(req); } Deno.serve( { port: PORT, hostname: "0.0.0.0", onListen: ({ port, hostname }) => { console.log(`Frontend-v2 server running on http://${hostname}:${port}`); if (isDevelopment) { console.log(`Proxying to Vite dev server at ${VITE_DEV_SERVER}`); } }, }, handler );