webapps: serve static files
This commit is contained in:
parent
0202ebb217
commit
682e0c29f3
|
|
@ -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<Response>
|
||||
export type App = Hono | Handler
|
||||
|
||||
export async function serveApp(c: Context, subdomain: string): Promise<Response> {
|
||||
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 | undefined> {
|
|||
app = await loadApp(join(NOSE_WWW, path))
|
||||
if (app) return app
|
||||
}
|
||||
|
||||
console.error("can't find app:", name)
|
||||
}
|
||||
|
||||
async function loadApp(path: string): Promise<App | undefined> {
|
||||
|
|
@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user