From 682e0c29f3b6053e282d0f5f1a0408da86e30017 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 28 Sep 2025 14:35:37 -0700 Subject: [PATCH] webapps: serve static files --- app/src/webapp.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/src/webapp.ts b/app/src/webapp.ts index e3b8737..6c35174 100644 --- a/app/src/webapp.ts +++ b/app/src/webapp.ts @@ -8,15 +8,23 @@ import { join, dirname } from "path" import { readdirSync } from "fs" import { NOSE_WWW } from "./config" -import { isFile } from "./utils" +import { isFile, isDir } from "./utils" export type Handler = (r: Context) => string | Child | Response | Promise export type App = Hono | Handler export async function serveApp(c: Context, subdomain: string): Promise { const app = await findApp(subdomain) + const path = appDir(subdomain) - if (!app) return c.text(`App Not Found: ${subdomain}`, 404) + if (!path) return c.text(`App not found: ${subdomain}`, 404) + + const staticPath = join(path, "pub", c.req.path === "/" ? "/index.html" : c.req.path) + + if (isFile(staticPath)) + return serveStatic(staticPath) + + if (!app) return c.text(`App not found: ${subdomain}`, 404) if (app instanceof Hono) return app.fetch(c.req.raw) @@ -62,8 +70,6 @@ async function findApp(name: string): Promise { app = await loadApp(join(NOSE_WWW, path)) if (app) return app } - - console.error("can't find app:", name) } async function loadApp(path: string): Promise { @@ -86,3 +92,12 @@ export function toResponse(source: string | Child | Response): Response { } }) } + +function serveStatic(path: string): Response { + const file = Bun.file(path) + return new Response(file, { + headers: { + "Content-Type": file.type + } + }) +} \ No newline at end of file