From 1ac15912b146e9ede744837c6dab46a414b068b4 Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 5 Sep 2023 18:05:29 +0200 Subject: [PATCH] Make server take into account GRIST_SERVERS (#659) Also ensure that only a home server will setup the database. resolves #654 Co-authored-by: Florent FAYOLLE --- README.md | 1 + app/server/mergedServerMain.ts | 2 +- stubs/app/server/server.ts | 42 ++++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index dc2b9de6..af392236 100644 --- a/README.md +++ b/README.md @@ -282,6 +282,7 @@ GRIST_PAGE_TITLE_SUFFIX | a string to append to the end of the `` in HTML GRIST_PROXY_AUTH_HEADER | header which will be set by a (reverse) proxy webserver with an authorized users' email. This can be used as an alternative to a SAML service. See also GRIST_FORWARD_AUTH_HEADER. GRIST_ROUTER_URL | optional url for an api that allows servers to be (un)registered with a load balancer GRIST_SERVE_SAME_ORIGIN | set to "true" to access home server and doc workers on the same protocol-host-port as the top-level page, same as for custom domains (careful, host header should be trustworthy) +GRIST_SERVERS | the types of server to setup. Comma separated values which may contain "home", "docs", static" and/or "app". Defaults to "home,docs,static". GRIST_SESSION_COOKIE | if set, overrides the name of Grist's cookie GRIST_SESSION_DOMAIN | if set, associates the cookie with the given domain - otherwise defaults to GRIST_DOMAIN GRIST_SESSION_SECRET | a key used to encode sessions diff --git a/app/server/mergedServerMain.ts b/app/server/mergedServerMain.ts index 6b640542..2512931b 100644 --- a/app/server/mergedServerMain.ts +++ b/app/server/mergedServerMain.ts @@ -15,7 +15,7 @@ export type ServerType = "home" | "docs" | "static" | "app"; const allServerTypes: ServerType[] = ["home", "docs", "static", "app"]; // Parse a comma-separate list of server types into an array, with validation. -function parseServerTypes(serverTypes: string|undefined): ServerType[] { +export function parseServerTypes(serverTypes: string|undefined): ServerType[] { // Split and filter out empty strings (including the one we get when splitting ""). const types = (serverTypes || "").trim().split(',').filter(part => Boolean(part)); diff --git a/stubs/app/server/server.ts b/stubs/app/server/server.ts index 9ea30b40..3ddaa422 100644 --- a/stubs/app/server/server.ts +++ b/stubs/app/server/server.ts @@ -34,7 +34,7 @@ if (!process.env.GRIST_SINGLE_ORG) { setDefaultEnv('GRIST_UI_FEATURES', 'helpCenter,billing,templates,multiSite,multiAccounts,sendToDrive'); setDefaultEnv('GRIST_WIDGET_LIST_URL', commonUrls.gristLabsWidgetRepository); import {updateDb} from 'app/server/lib/dbUtils'; -import {main as mergedServerMain} from 'app/server/mergedServerMain'; +import {main as mergedServerMain, parseServerTypes} from 'app/server/mergedServerMain'; import * as fse from 'fs-extra'; const G = { @@ -48,19 +48,7 @@ function setDefaultEnv(name: string, value: string) { } } -// tslint:disable:no-console -export async function main() { - console.log('Welcome to Grist.'); - if (!debugging) { - console.log(`In quiet mode, see http://localhost:${G.port} to use.`); - console.log('For full logs, re-run with DEBUG=1'); - } - - // If SAML is not configured, there's no login system, so provide a default email address. - setDefaultEnv('GRIST_DEFAULT_EMAIL', 'you@example.com'); - // Set directory for uploaded documents. - setDefaultEnv('GRIST_DATA_DIR', 'docs'); - await fse.mkdirp(process.env.GRIST_DATA_DIR!); +async function setupDb() { // Make a blank db if needed. if (process.env.TEST_CLEAN_DATABASE) { const {createInitialDb} = require('test/gen-server/seed'); @@ -104,9 +92,33 @@ export async function main() { })); } } +} + +// tslint:disable:no-console +export async function main() { + console.log('Welcome to Grist.'); + if (!debugging) { + console.log(`In quiet mode, see http://localhost:${G.port} to use.`); + console.log('For full logs, re-run with DEBUG=1'); + } + + // If SAML is not configured, there's no login system, so provide a default email address. + setDefaultEnv('GRIST_DEFAULT_EMAIL', 'you@example.com'); + // Set directory for uploaded documents. + setDefaultEnv('GRIST_DATA_DIR', 'docs'); + setDefaultEnv('GRIST_SERVERS', 'home,docs,static'); + const serverTypes = parseServerTypes(process.env.GRIST_SERVERS); + + await fse.mkdirp(process.env.GRIST_DATA_DIR!); + + if (serverTypes.includes("home")) { + console.log('Setting up database...'); + await setupDb(); + console.log('Database setup complete.'); + } // Launch single-port, self-contained version of Grist. - const server = await mergedServerMain(G.port, ["home", "docs", "static"]); + const server = await mergedServerMain(G.port, serverTypes); if (process.env.GRIST_TESTING_SOCKET) { await server.addTestingHooks(); }