From e251c2087bef150dfdbe468da3d96856e7ca5e0d Mon Sep 17 00:00:00 2001 From: Florent FAYOLLE Date: Thu, 22 Feb 2024 16:46:09 +0100 Subject: [PATCH] Introduce APP_HOME_INTERNAL_URL --- README.md | 3 ++- app/server/lib/FlexServer.ts | 18 ++++++++++++++++-- app/server/lib/GristServer.ts | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c950d741..66539273 100644 --- a/README.md +++ b/README.md @@ -237,8 +237,9 @@ Grist can be configured in many ways. Here are the main environment variables it |------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ALLOWED_WEBHOOK_DOMAINS | comma-separated list of permitted domains to use in webhooks (e.g. webhook.site,zapier.com). You can set this to `*` to allow all domains, but if doing so, we recommend using a carefully locked-down proxy (see `GRIST_HTTPS_PROXY`) if you do not entirely trust users. Otherwise services on your internal network may become vulnerable to manipulation. | | APP_DOC_URL | doc worker url, set when starting an individual doc worker (other servers will find doc worker urls via redis) | -| APP_DOC_INTERNAL_URL | like `APP_DOC_URL` but used by the home server to reach the server using an internal domain name resolution (like in a docker environment). Defaults to `APP_DOC_URL` | +| APP_DOC_INTERNAL_URL | like `APP_DOC_URL` but used by the home server to reach the server using an internal domain name resolution (like in a docker environment). You may only define this value in the doc worker. Defaults to `APP_DOC_URL`. | | APP_HOME_URL | url prefix for home api (home and doc servers need this) | +| APP_HOME_INTERNAL_URL | like `APP_HOME_URL` but used by the home server to reach any home workers using an internal domain name resolution (like in a docker environment). Defaults to `APP_HOME_URL` | | APP_STATIC_URL | url prefix for static resources | | APP_STATIC_INCLUDE_CUSTOM_CSS | set to "true" to include custom.css (from APP_STATIC_URL) in static pages | | APP_UNTRUSTED_URL | URL at which to serve/expect plugin content. | diff --git a/app/server/lib/FlexServer.ts b/app/server/lib/FlexServer.ts index 17ac46a3..ec623e1b 100644 --- a/app/server/lib/FlexServer.ts +++ b/app/server/lib/FlexServer.ts @@ -295,6 +295,13 @@ export class FlexServer implements GristServer { return homeUrl; } + /** + * Same as getDefaultHomeUrl, but for internal use. + */ + public getDefaultHomeInternalUrl(): string { + return process.env.APP_HOME_INTERNAL_URL || this.getDefaultHomeUrl(); + } + /** * Get a url for the home server api, adapting it to match the base domain in the * requested url. This adaptation is important for cookie-based authentication. @@ -302,13 +309,20 @@ export class FlexServer implements GristServer { * If relPath is given, returns that path relative to homeUrl. If omitted, note that * getHomeUrl() will still return a URL ending in "/". */ - public getHomeUrl(req: express.Request, relPath: string = ''): string { + public getHomeUrl(req: express.Request, relPath: string = '', defaultHomeUrl = this.getDefaultHomeUrl()): string { // Get the default home url. - const homeUrl = new URL(relPath, this.getDefaultHomeUrl()); + const homeUrl = new URL(relPath, defaultHomeUrl); adaptServerUrl(homeUrl, req as RequestWithOrg); return homeUrl.href; } + /** + * Same as getHomeUrl, but for requesting internally. + */ + public getHomeInternalUrl(req: express.Request, relPath?: string): string { + return this.getHomeUrl(req, relPath, this.getDefaultHomeInternalUrl()); + } + /** * Get a home url that is appropriate for the given document. For now, this * returns a default that works for all documents. That could change in future, diff --git a/app/server/lib/GristServer.ts b/app/server/lib/GristServer.ts index 9c53f347..125dffc3 100644 --- a/app/server/lib/GristServer.ts +++ b/app/server/lib/GristServer.ts @@ -35,6 +35,7 @@ export interface GristServer { settings?: Readonly>; getHost(): string; getHomeUrl(req: express.Request, relPath?: string): string; + getHomeInternalUrl(req: express.Request, relPath?: string): string; getHomeUrlByDocId(docId: string, relPath?: string): Promise; getOwnUrl(): string; getOrgUrl(orgKey: string|number): Promise; @@ -127,6 +128,7 @@ export function createDummyGristServer(): GristServer { settings: {}, getHost() { return 'localhost:4242'; }, getHomeUrl() { return 'http://localhost:4242'; }, + getHomeInternalUrl() { return 'http://localhost:4242'; }, getHomeUrlByDocId() { return Promise.resolve('http://localhost:4242'); }, getMergedOrgUrl() { return 'http://localhost:4242'; }, getOwnUrl() { return 'http://localhost:4242'; },