Add base path to hono
parent
0edd49ec5a
commit
fff01410d5
|
@ -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 => {
|
export const toRealUrl = <T extends (string | URL)>(url : T) : T => {
|
||||||
if (typeof url === 'string') {
|
if (typeof url === 'string') {
|
||||||
|
@ -36,13 +36,14 @@ export const toInternalUrl = (url : string) => {
|
||||||
return parsedUrl.href;
|
return parsedUrl.href;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
console.log("Don't know how to deal with non-prefix: ", url);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
} else if (url.startsWith(urlPrefix)) {
|
} else if (url.startsWith(urlPrefix)) {
|
||||||
return url.substr(urlPrefix.length);
|
return url.substr(urlPrefix.length);
|
||||||
}
|
}
|
||||||
else {
|
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;
|
return url;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,7 +20,7 @@ import {
|
||||||
parsePageRef,
|
parsePageRef,
|
||||||
} from "@silverbulletmd/silverbullet/lib/page_ref";
|
} from "@silverbulletmd/silverbullet/lib/page_ref";
|
||||||
import { base64Encode } from "$lib/crypto.ts";
|
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
|
const authenticationExpirySeconds = 60 * 60 * 24 * 7; // 1 week
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ export class HttpServer {
|
||||||
baseKvPrimitives: KvPrimitives;
|
baseKvPrimitives: KvPrimitives;
|
||||||
|
|
||||||
constructor(private options: ServerOptions) {
|
constructor(private options: ServerOptions) {
|
||||||
this.app = new Hono();
|
this.app = new Hono().basePath(urlPrefix);
|
||||||
this.clientAssetBundle = options.clientAssetBundle;
|
this.clientAssetBundle = options.clientAssetBundle;
|
||||||
this.plugAssetBundle = options.plugAssetBundle;
|
this.plugAssetBundle = options.plugAssetBundle;
|
||||||
this.hostname = options.hostname;
|
this.hostname = options.hostname;
|
||||||
|
@ -153,7 +153,7 @@ export class HttpServer {
|
||||||
|
|
||||||
// Fallback, serve the UI index.html
|
// Fallback, serve the UI index.html
|
||||||
this.app.use("*", (c) => {
|
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));
|
const pageName = decodePageURI(url.pathname.slice(1));
|
||||||
return this.renderHtmlPage(this.spaceServer, pageName, c);
|
return this.renderHtmlPage(this.spaceServer, pageName, c);
|
||||||
});
|
});
|
||||||
|
@ -186,7 +186,7 @@ export class HttpServer {
|
||||||
serveCustomEndpoints() {
|
serveCustomEndpoints() {
|
||||||
this.app.use("/_/*", async (ctx) => {
|
this.app.use("/_/*", async (ctx) => {
|
||||||
const req = ctx.req;
|
const req = ctx.req;
|
||||||
const url = new URL(req.url);
|
const url = new URL(toInternalUrl(req.url));
|
||||||
if (!this.spaceServer.serverSystem) {
|
if (!this.spaceServer.serverSystem) {
|
||||||
return ctx.text("No server system available", 500);
|
return ctx.text("No server system available", 500);
|
||||||
}
|
}
|
||||||
|
@ -244,7 +244,7 @@ export class HttpServer {
|
||||||
serveStatic() {
|
serveStatic() {
|
||||||
this.app.use("*", (c, next): Promise<void | Response> => {
|
this.app.use("*", (c, next): Promise<void | Response> => {
|
||||||
const req = c.req;
|
const req = c.req;
|
||||||
const url = new URL(req.url);
|
const url = new URL(toInternalUrl(req.url));
|
||||||
// console.log("URL", url);
|
// console.log("URL", url);
|
||||||
if (
|
if (
|
||||||
url.pathname === "/"
|
url.pathname === "/"
|
||||||
|
@ -327,7 +327,7 @@ export class HttpServer {
|
||||||
|
|
||||||
// TODO: This should probably be a POST request
|
// TODO: This should probably be a POST request
|
||||||
this.app.get("/.logout", (c) => {
|
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));
|
deleteCookie(c, authCookieName(url.host));
|
||||||
|
|
||||||
return c.redirect(toRealUrl("/.auth"));
|
return c.redirect(toRealUrl("/.auth"));
|
||||||
|
@ -357,7 +357,7 @@ export class HttpServer {
|
||||||
}),
|
}),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const req = c.req;
|
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 { username, password } = req.valid("form");
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
@ -400,7 +400,7 @@ export class HttpServer {
|
||||||
// Auth disabled in this config, skip
|
// Auth disabled in this config, skip
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
const url = new URL(req.url);
|
const url = new URL(toInternalUrl(req.url));
|
||||||
const host = url.host;
|
const host = url.host;
|
||||||
const redirectToAuth = () => {
|
const redirectToAuth = () => {
|
||||||
// Try filtering api paths
|
// Try filtering api paths
|
||||||
|
|
Loading…
Reference in New Issue