Skip to content

A local-first personal finance app

Improve the server config (#120)

Jed Fox9b5fb43ad376
diff --git a/app.js b/app.js
index db3aa578d6..944d33aa41 100644
--- a/app.js
+++ b/app.js
@@ -34,14 +34,10 @@ app.use((req, res, next) => {
res.set('Cross-Origin-Embedder-Policy', 'require-corp');
next();
});
-app.use(
- express.static(__dirname + '/node_modules/@actual-app/web/build', {
- index: false
- })
-);
-app.get('/*', (req, res) => {
- res.sendFile(__dirname + '/node_modules/@actual-app/web/build/index.html');
-});
+app.use(express.static(config.webRoot, { index: false }));
+
+let indexHTML = fs.readFileSync(config.webRoot + '/index.html');
+app.get('/*', (req, res) => res.send(indexHTML));
async function run() {
if (!fs.existsSync(config.serverFiles)) {
@@ -55,18 +51,22 @@ async function run() {
await accountApp.init();
await syncApp.init();
- console.log('Listening on ' + config.hostname + ':' + config.port + '...');
if (config.https) {
const https = require('https');
const httpsOptions = {
...config.https,
- key: fs.readFileSync(config.https.key),
- cert: fs.readFileSync(config.https.cert)
+ key: config.https.key.startsWith('-----BEGIN')
+ ? config.https.key
+ : fs.readFileSync(config.https.key),
+ cert: config.https.cert.startsWith('-----BEGIN')
+ ? config.https.cert
+ : fs.readFileSync(config.https.cert)
};
https.createServer(httpsOptions, app).listen(config.port, config.hostname);
} else {
app.listen(config.port, config.hostname);
}
+ console.log('Listening on ' + config.hostname + ':' + config.port + '...');
}
run().catch((err) => {
diff --git a/config-types.ts b/config-types.ts
new file mode 100644
index 0000000000..41eb188659
--- /dev/null
+++ b/config-types.ts
@@ -0,0 +1,12 @@
+export interface Config {
+ mode: 'test' | 'development';
+ port: number;
+ hostname: string;
+ serverFiles: string;
+ userFiles: string;
+ webRoot: string;
+ https?: {
+ key: string;
+ cert: string;
+ } & Parameters<typeof import('node:https')['createServer']>[0];
+}
diff --git a/load-config.js b/load-config.js
index 7ced9689cf..51e1773ec8 100644
--- a/load-config.js
+++ b/load-config.js
@@ -1,35 +1,58 @@
-let config = {};
+let userConfig;
let fs = require('fs');
let { join } = require('path');
let root = fs.existsSync('/data') ? '/data' : __dirname;
-try {
- // @ts-expect-error TS2307: we expect this file may not exist
- config = require('./config');
-} catch (e) {
- // do nothing
+if (process.env.ACTUAL_CONFIG_PATH) {
+ userConfig = require(process.env.ACTUAL_CONFIG_PATH);
+} else {
+ try {
+ // @ts-expect-error TS2307: we expect this file may not exist
+ userConfig = require('./config');
+ } catch (e) {
+ // do nothing
+ }
}
+/** @type {Omit<import('./config-types').Config, 'mode' | 'serverFiles' | 'userFiles'>} */
+let defaultConfig = {
+ port: 5006,
+ hostname: '::',
+ webRoot: join(__dirname, 'node_modules', '@actual-app', 'web', 'build')
+};
+
+/** @type {import('./config-types').Config} */
+let config;
if (process.env.NODE_ENV === 'test') {
config = {
mode: 'test',
- port: 5006,
- hostname: '::',
serverFiles: join(__dirname, 'test-server-files'),
- userFiles: join(__dirname, 'test-user-files')
+ userFiles: join(__dirname, 'test-user-files'),
+ ...defaultConfig
};
} else {
config = {
mode: 'development',
- port: 5006,
- hostname: '::',
+ ...defaultConfig,
serverFiles: join(root, 'server-files'),
userFiles: join(root, 'user-files'),
- ...config
+ ...(userConfig || {})
};
}
-// The env variable always takes precedence
-config.userFiles = process.env.ACTUAL_USER_FILES || config.userFiles;
-
-module.exports = config;
+module.exports = {
+ ...config,
+ port: +process.env.ACTUAL_PORT || +process.env.PORT || config.port,
+ hostname: process.env.ACTUAL_HOSTNAME || config.hostname,
+ serverFiles: process.env.ACTUAL_SERVER_FILES || config.serverFiles,
+ userFiles: process.env.ACTUAL_USER_FILES || config.userFiles,
+ webRoot: process.env.ACTUAL_WEB_ROOT || config.webRoot,
+ https:
+ process.env.ACTUAL_HTTPS_KEY && process.env.ACTUAL_HTTPS_CERT
+ ? {
+ key: process.env.ACTUAL_HTTPS_KEY,
+ cert: process.env.ACTUAL_HTTPS_CERT,
+ ...(config.https || {})
+ }
+ : config.https
+};