rspace-online/modules/funds/standalone.ts

46 lines
1.2 KiB
TypeScript

/**
* Funds standalone server — independent deployment at rfunds.online.
*/
import { Hono } from "hono";
import { resolve } from "node:path";
import { fundsModule } from "./mod";
const PORT = Number(process.env.PORT) || 3000;
const DIST_DIR = resolve(import.meta.dir, "../../dist");
const app = new Hono();
app.get("/.well-known/webauthn", (c) => {
return c.json(
{ origins: ["https://rspace.online"] },
200,
{ "Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=3600" }
);
});
app.route("/", fundsModule.routes);
Bun.serve({
port: PORT,
async fetch(req) {
const url = new URL(req.url);
if (url.pathname !== "/" && !url.pathname.startsWith("/api/")) {
const assetPath = url.pathname.slice(1);
if (assetPath.includes(".")) {
const file = Bun.file(resolve(DIST_DIR, assetPath));
if (await file.exists()) {
const ct = assetPath.endsWith(".js") ? "application/javascript" :
assetPath.endsWith(".css") ? "text/css" :
assetPath.endsWith(".html") ? "text/html" :
"application/octet-stream";
return new Response(file, { headers: { "Content-Type": ct } });
}
}
}
return app.fetch(req);
},
});
console.log(`rFunds standalone server running on http://localhost:${PORT}`);