2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* A version of hosted grist that recombines a home server,
|
|
|
|
* a doc worker, and a static server on a single port.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {FlexServer, FlexServerOptions} from 'app/server/lib/FlexServer';
|
2023-02-13 20:52:17 +00:00
|
|
|
import {GristLoginSystem} from 'app/server/lib/GristServer';
|
2022-07-04 14:14:55 +00:00
|
|
|
import log from 'app/server/lib/log';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
// Allowed server types. We'll start one or a combination based on the value of GRIST_SERVERS
|
|
|
|
// environment variable.
|
|
|
|
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[] {
|
|
|
|
// Split and filter out empty strings (including the one we get when splitting "").
|
|
|
|
const types = (serverTypes || "").trim().split(',').filter(part => Boolean(part));
|
|
|
|
|
|
|
|
// Check that parts is non-empty and only contains valid options.
|
|
|
|
if (!types.length) {
|
|
|
|
throw new Error(`No server types; should be a comma-separated list of ${allServerTypes.join(", ")}`);
|
|
|
|
}
|
|
|
|
for (const t of types) {
|
|
|
|
if (!allServerTypes.includes(t as ServerType)) {
|
|
|
|
throw new Error(`Invalid server type '${t}'; should be in ${allServerTypes.join(", ")}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return types as ServerType[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ServerOptions extends FlexServerOptions {
|
|
|
|
logToConsole?: boolean; // If set, messages logged to console (default: false)
|
|
|
|
// (but if options are not given at all in call to main,
|
|
|
|
// logToConsole is set to true)
|
2022-06-03 14:54:49 +00:00
|
|
|
externalStorage?: boolean; // If set, documents saved to external storage such as s3 (default is to check environment
|
2020-07-21 13:20:51 +00:00
|
|
|
// variables, which get set in various ways in dev/test entry points)
|
2023-02-13 20:52:17 +00:00
|
|
|
loginSystem?: () => Promise<GristLoginSystem>;
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a server on the given port, including the functionality specified in serverTypes.
|
|
|
|
*/
|
|
|
|
export async function main(port: number, serverTypes: ServerType[],
|
2023-02-13 20:52:17 +00:00
|
|
|
options: ServerOptions = {}) {
|
2020-07-21 13:20:51 +00:00
|
|
|
const includeHome = serverTypes.includes("home");
|
|
|
|
const includeDocs = serverTypes.includes("docs");
|
|
|
|
const includeStatic = serverTypes.includes("static");
|
|
|
|
const includeApp = serverTypes.includes("app");
|
|
|
|
|
|
|
|
const server = new FlexServer(port, `server(${serverTypes.join(",")})`, options);
|
|
|
|
|
2023-02-13 20:52:17 +00:00
|
|
|
if (options.loginSystem) {
|
|
|
|
server.setLoginSystem(options.loginSystem);
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
server.addCleanup();
|
|
|
|
server.setDirectory();
|
|
|
|
|
|
|
|
if (process.env.GRIST_TEST_ROUTER) {
|
|
|
|
// Add a mock api for adding/removing doc workers from load balancer.
|
|
|
|
server.testAddRouter();
|
|
|
|
}
|
|
|
|
|
2023-02-13 20:52:17 +00:00
|
|
|
if (options.logToConsole !== false) { server.addLogging(); }
|
2022-06-03 14:54:49 +00:00
|
|
|
if (options.externalStorage === false) { server.disableExternalStorage(); }
|
2020-07-21 13:20:51 +00:00
|
|
|
await server.loadConfig();
|
|
|
|
|
|
|
|
if (includeDocs) {
|
|
|
|
// It is important that /dw and /v prefixes are accepted (if present) by health check
|
|
|
|
// in this case, since they are included in the url registered for the doc worker.
|
|
|
|
server.stripDocWorkerIdPathPrefixIfPresent();
|
|
|
|
server.addTagChecker();
|
|
|
|
}
|
|
|
|
|
|
|
|
server.addHealthCheck();
|
|
|
|
|
|
|
|
if (includeHome || includeStatic || includeApp) {
|
|
|
|
server.setDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeHome || includeStatic) {
|
|
|
|
server.addStaticAndBowerDirectories();
|
|
|
|
}
|
|
|
|
|
|
|
|
await server.initHomeDBManager();
|
|
|
|
server.addHosts();
|
|
|
|
|
|
|
|
server.addDocWorkerMap();
|
|
|
|
|
|
|
|
if (includeHome || includeStatic) {
|
|
|
|
await server.addAssetsForPlugins();
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:11:11 +00:00
|
|
|
if (includeHome) {
|
2020-07-21 13:20:51 +00:00
|
|
|
server.addEarlyWebhooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeHome || includeDocs || includeApp) {
|
|
|
|
server.addSessions();
|
|
|
|
}
|
|
|
|
|
|
|
|
server.addAccessMiddleware();
|
|
|
|
server.addApiMiddleware();
|
2022-05-11 19:05:35 +00:00
|
|
|
await server.addBillingMiddleware();
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
await server.start();
|
|
|
|
|
|
|
|
if (includeHome) {
|
2022-07-25 20:11:11 +00:00
|
|
|
server.addUsage();
|
2020-07-21 13:20:51 +00:00
|
|
|
if (!includeDocs) {
|
|
|
|
server.addDocApiForwarder();
|
|
|
|
}
|
|
|
|
server.addJsonSupport();
|
2021-08-05 15:12:46 +00:00
|
|
|
await server.addLandingPages();
|
2020-07-21 13:20:51 +00:00
|
|
|
// todo: add support for home api to standalone app
|
2022-07-25 20:11:11 +00:00
|
|
|
server.addHomeApi();
|
|
|
|
server.addBillingApi();
|
|
|
|
server.addNotifier();
|
2023-07-04 21:21:34 +00:00
|
|
|
await server.addTelemetry();
|
2022-07-25 20:11:11 +00:00
|
|
|
await server.addHousekeeper();
|
2021-08-17 15:22:30 +00:00
|
|
|
await server.addLoginRoutes();
|
2022-01-07 18:11:52 +00:00
|
|
|
server.addAccountPage();
|
2020-07-21 13:20:51 +00:00
|
|
|
server.addBillingPages();
|
|
|
|
server.addWelcomePaths();
|
|
|
|
server.addLogEndpoint();
|
2021-07-21 08:46:03 +00:00
|
|
|
server.addGoogleAuthEndpoint();
|
2023-07-04 21:21:34 +00:00
|
|
|
server.addInstallEndpoints();
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeDocs) {
|
|
|
|
server.addJsonSupport();
|
2023-07-04 21:21:34 +00:00
|
|
|
await server.addTelemetry();
|
2020-07-21 13:20:51 +00:00
|
|
|
await server.addDoc();
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:29:38 +00:00
|
|
|
if (includeHome) {
|
|
|
|
server.addClientSecrets();
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
server.finalize();
|
|
|
|
|
2023-05-23 16:51:58 +00:00
|
|
|
server.checkOptionCombinations();
|
2020-07-21 13:20:51 +00:00
|
|
|
server.summary();
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function startMain() {
|
|
|
|
try {
|
|
|
|
const serverTypes = parseServerTypes(process.env.GRIST_SERVERS);
|
|
|
|
|
|
|
|
// No defaults for a port, since this server can serve very different purposes.
|
|
|
|
if (!process.env.GRIST_PORT) {
|
|
|
|
throw new Error("GRIST_PORT must be specified");
|
|
|
|
}
|
|
|
|
const port = parseInt(process.env.GRIST_PORT, 10);
|
|
|
|
|
|
|
|
const server = await main(port, serverTypes);
|
|
|
|
|
|
|
|
const opt = process.argv[2];
|
|
|
|
if (opt === '--testingHooks') {
|
|
|
|
await server.addTestingHooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
return server;
|
|
|
|
} catch (e) {
|
|
|
|
log.error('mergedServer failed to start', e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
startMain().catch((e) => log.error('mergedServer failed to start', e));
|
|
|
|
}
|