Skip to content

Commit

Permalink
support dotenv, GRIST_HOST, PORT
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfitz committed Jan 27, 2023
1 parent 7c4241d commit d845bb9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 81 deletions.
47 changes: 30 additions & 17 deletions ext/app/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as dotenv from "dotenv";
import * as version from 'app/common/version';
import * as log from 'app/server/lib/log';
import * as electron from 'electron';
Expand All @@ -6,6 +7,8 @@ import * as os from 'os';
import * as path from 'path';
import * as winston from 'winston';

dotenv.config();

// Handle --version flag, which causes us to only print version, without running anything.
if (process.argv.includes('--version')) {
console.log(`${version.version} (${version.gitcommit} on ${version.channel})`);
Expand Down Expand Up @@ -34,7 +37,7 @@ import { updateDb } from 'app/server/lib/dbUtils';
import { FlexServer } from 'app/server/lib/FlexServer';
import * as serverUtils from 'app/server/lib/serverUtils';
import * as shutdown from 'app/server/lib/shutdown';
import * as server from 'app/electron/server';
import { main as mergedServerMain } from 'app/server/mergedServerMain';

const RecentItems = require('app/common/RecentItems');

Expand Down Expand Up @@ -102,7 +105,7 @@ class GristApp {

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
this.app.on('ready', () => this.onReady());
this.app.on('ready', () => this.onReady().catch(reportErrorAndStop));
}

private onInstanceStart(argv: any, workingDir: any) {
Expand Down Expand Up @@ -308,25 +311,30 @@ class GristApp {
}

private async onReady() {
let hostPort: string | number = 47478;
let untrustedPort = 47479;
// The port doesn't matter, we only care to avoid interfering with something that another
// application might want after we have bound it. We pick 47478 (phone number for GRIST).
hostPort = process.env.GRIST_TEST_PORT || await serverUtils.getAvailablePort(hostPort);
const port = process.env.PORT || process.env.GRIST_PORT ||
await serverUtils.getAvailablePort(47478);
// get available port for untrusted content
untrustedPort = await serverUtils.getAvailablePort(untrustedPort);
this.appHost = "http://localhost:" + hostPort;
const untrustedPort = process.env.UNTRUSTED_PORT ||
process.env.GRIST_UNTRUSTED_PORT ||
await serverUtils.getAvailablePort(47479);

const host = setDefaultEnv('GRIST_HOST', 'localhost');
setDefaultEnv('PORT', String(port));
this.appHost = `http://${host}:${port}`;
setDefaultEnv('APP_HOME_URL', this.appHost);
// TODO: check trust in electron scenario, this code is very rusty.
setDefaultEnv('APP_UNTRUSTED_URL', `http://${host}:${untrustedPort}`);

await updateDb();

this.flexServer = await server.start({
userRoot: process.env.GRIST_USER_ROOT ||path.join(os.homedir(), '.grist'),
docsRoot: process.env.GRIST_DATA_DIR || this.app.getPath('documents'),
instanceRoot: path.join(process.env.GRIST_USER_DATA_DIR || this.app.getPath('userData')),
host: 'localhost',
port: hostPort,
untrustedContent : process.env.APP_UNTRUSTED_URL || `http://localhost:${untrustedPort}`,
serverMode: "electron",
});

setDefaultEnv('GRIST_USER_ROOT', path.join(os.homedir(), '.grist'));
setDefaultEnv('GRIST_DATA_DIR', this.app.getPath('documents'));
setDefaultEnv('GRIST_INST_DIR', process.env.GRIST_USER_DATA_DIR || this.app.getPath('userData'));
this.flexServer = await mergedServerMain(
parseInt(String(port), 10),
['home', 'docs', 'static', 'app']);
const serverMethods = this.flexServer.electronServerMethods;
// This function is what we'll call now, and also in onInstanceStart. The latter is used on
// Windows thanks to makeSingleInstance, and triggered when user clicks another .grist file.
Expand Down Expand Up @@ -452,3 +460,8 @@ function setDefaultEnv(name: string, value: string) {
process.env[name] = value;
return value;
}

function reportErrorAndStop(e: Error) {
console.error(e);
process.exit(1);
}
64 changes: 0 additions & 64 deletions ext/app/electron/server.ts

This file was deleted.

1 change: 1 addition & 0 deletions ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"aws-sdk": "2.1061.0",
"@azure/storage-blob": "12.9.0",
"dotenv": "16.0.3",
"electron": "22.0.3",
"electron-updater": "^5.3.0"
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,11 @@ dotenv-expand@^5.1.0:
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==

[email protected]:
version "16.0.3"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==

dotenv@^16.0.0:
version "16.0.2"
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.0.2.tgz"
Expand Down

0 comments on commit d845bb9

Please sign in to comment.