Skip to content

Commit

Permalink
Add base path to hono
Browse files Browse the repository at this point in the history
  • Loading branch information
Shihira committed Oct 26, 2024
1 parent 0edd49e commit fff0141
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/url_hack.ts
Original file line number Diff line number Diff line change
@@ -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 = <T extends (string | URL)>(url : T) : T => {
if (typeof url === 'string') {
Expand Down Expand Up @@ -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;
}
};
Expand Down
16 changes: 8 additions & 8 deletions server/http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ export class HttpServer {
serveStatic() {
this.app.use("*", (c, next): Promise<void | Response> => {
const req = c.req;
const url = new URL(req.url);
const url = new URL(toInternalUrl(req.url));
// console.log("URL", url);
if (
url.pathname === "/"
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fff0141

Please sign in to comment.