From fff01410d5f15b0a2bfc1bf45d434fffbecd77c9 Mon Sep 17 00:00:00 2001 From: Shihira Fung Date: Sun, 27 Oct 2024 02:45:52 +0800 Subject: [PATCH] Add base path to hono --- lib/url_hack.ts | 5 +++-- server/http_server.ts | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/url_hack.ts b/lib/url_hack.ts index fe9bafa5..abc43e3a 100644 --- a/lib/url_hack.ts +++ b/lib/url_hack.ts @@ -1,4 +1,4 @@ -export const urlPrefix = Deno.env.get('SB_URL_PREFIX') ?? (globalThis.silverBulletConfig ? globalThis.silverBulletConfig.urlPrefix : null) ?? ''; +export const urlPrefix : string = Deno.env.get('SB_URL_PREFIX') ?? (globalThis.silverBulletConfig ? globalThis.silverBulletConfig.urlPrefix : null) ?? ''; export const toRealUrl = (url : T) : T => { if (typeof url === 'string') { @@ -36,13 +36,14 @@ export const toInternalUrl = (url : string) => { return parsedUrl.href; } else { + console.log("Don't know how to deal with non-prefix: ", url); return url; } } else if (url.startsWith(urlPrefix)) { return url.substr(urlPrefix.length); } else { - console.log("Don't know how to deal with relative path: ", url); + console.log("Don't know how to deal with non-prefix: ", url); return url; } }; diff --git a/server/http_server.ts b/server/http_server.ts index 2b5b18bf..f866a46d 100644 --- a/server/http_server.ts +++ b/server/http_server.ts @@ -20,7 +20,7 @@ import { parsePageRef, } from "@silverbulletmd/silverbullet/lib/page_ref"; import { base64Encode } from "$lib/crypto.ts"; -import { urlPrefix, toRealUrl } from "$lib/url_hack.ts"; +import { urlPrefix, toRealUrl, toInternalUrl } from "$lib/url_hack.ts"; const authenticationExpirySeconds = 60 * 60 * 24 * 7; // 1 week @@ -59,7 +59,7 @@ export class HttpServer { baseKvPrimitives: KvPrimitives; constructor(private options: ServerOptions) { - this.app = new Hono(); + this.app = new Hono().basePath(urlPrefix); this.clientAssetBundle = options.clientAssetBundle; this.plugAssetBundle = options.plugAssetBundle; this.hostname = options.hostname; @@ -153,7 +153,7 @@ export class HttpServer { // Fallback, serve the UI index.html this.app.use("*", (c) => { - const url = new URL(c.req.url); + const url = new URL(toInternalUrl(c.req.url)); const pageName = decodePageURI(url.pathname.slice(1)); return this.renderHtmlPage(this.spaceServer, pageName, c); }); @@ -186,7 +186,7 @@ export class HttpServer { serveCustomEndpoints() { this.app.use("/_/*", async (ctx) => { const req = ctx.req; - const url = new URL(req.url); + const url = new URL(toInternalUrl(req.url)); if (!this.spaceServer.serverSystem) { return ctx.text("No server system available", 500); } @@ -244,7 +244,7 @@ export class HttpServer { serveStatic() { this.app.use("*", (c, next): Promise => { const req = c.req; - const url = new URL(req.url); + const url = new URL(toInternalUrl(req.url)); // console.log("URL", url); if ( url.pathname === "/" @@ -327,7 +327,7 @@ export class HttpServer { // TODO: This should probably be a POST request this.app.get("/.logout", (c) => { - const url = new URL(c.req.url); + const url = new URL(toInternalUrl(c.req.url)); deleteCookie(c, authCookieName(url.host)); return c.redirect(toRealUrl("/.auth")); @@ -357,7 +357,7 @@ export class HttpServer { }), async (c) => { const req = c.req; - const url = new URL(c.req.url); + const url = new URL(toInternalUrl(c.req.url)); const { username, password } = req.valid("form"); const { @@ -400,7 +400,7 @@ export class HttpServer { // Auth disabled in this config, skip return next(); } - const url = new URL(req.url); + const url = new URL(toInternalUrl(req.url)); const host = url.host; const redirectToAuth = () => { // Try filtering api paths