A local-first personal finance app
Move source code to an src/ directory (#131)
Jed Fox76f398dd16ea
diff --git a/app.js b/app.jsindex b2f70aef89..3f6034b3cb 100644--- a/app.js+++ b/app.js@@ -1,75 +1,4 @@-const fs = require('fs');-const express = require('express');-const actuator = require('express-actuator');-const bodyParser = require('body-parser');-const cors = require('cors');-const config = require('./load-config');--const accountApp = require('./app-account');-const syncApp = require('./app-sync');--const app = express();--process.on('unhandledRejection', (reason) => {- console.log('Rejection:', reason);-});--app.use(cors());-app.use(bodyParser.json({ limit: '20mb' }));-app.use(bodyParser.raw({ type: 'application/actual-sync', limit: '20mb' }));-app.use(bodyParser.raw({ type: 'application/encrypted-file', limit: '50mb' }));--app.use('/sync', syncApp.handlers);-app.use('/account', accountApp.handlers);--app.get('/mode', (req, res) => {- res.send(config.mode);-});--app.use(actuator()); // Provides /health, /metrics, /info--// The web frontend-app.use((req, res, next) => {- res.set('Cross-Origin-Opener-Policy', 'same-origin');- res.set('Cross-Origin-Embedder-Policy', 'require-corp');- next();-});-app.use(express.static(config.webRoot, { index: false }));--app.get('/*', (req, res) => res.sendFile(config.webRoot + '/index.html'));--function parseHTTPSConfig(value) {- if (value.startsWith('-----BEGIN')) {- return value;- }- return fs.readFileSync(value);-}--async function run() {- if (!fs.existsSync(config.serverFiles)) {- fs.mkdirSync(config.serverFiles);- }-- if (!fs.existsSync(config.userFiles)) {- fs.mkdirSync(config.userFiles);- }-- await accountApp.init();- await syncApp.init();-- if (config.https) {- const https = require('https');- const httpsOptions = {- ...config.https,- key: parseHTTPSConfig(config.https.key),- cert: parseHTTPSConfig(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 + '...');-}+const run = require('./src/app');run().catch((err) => {console.log('Error starting app:', err);diff --git a/jest.setup.js b/jest.setup.jsindex 2f0a385072..3946ba0ae6 100644--- a/jest.setup.js+++ b/jest.setup.js@@ -1,7 +1,7 @@const fs = require('fs');const { join } = require('path');-const { getAccountDb } = require('./account-db');-const config = require('./load-config');+const { getAccountDb } = require('./src/account-db');+const config = require('./src/load-config');// Delete previous test database (force creation of a new one)const dbPath = join(config.serverFiles, 'account.sqlite');diff --git a/account-db.js b/src/account-db.jssimilarity index 84%rename from account-db.jsrename to src/account-db.jsindex 8e60efd401..e4213dba4d 100644--- a/account-db.js+++ b/src/account-db.js@@ -17,7 +17,10 @@ function getAccountDb() {accountDb = openDatabase(dbPath);if (needsInit) {- let initSql = fs.readFileSync(join(__dirname, 'sql/account.sql'), 'utf8');+ let initSql = fs.readFileSync(+ join(__dirname, '../sql/account.sql'),+ 'utf8'+ );accountDb.exec(initSql);}}diff --git a/app-account.js b/src/app-account.jssimilarity index 100%rename from app-account.jsrename to src/app-account.jsdiff --git a/app-sync.js b/src/app-sync.jssimilarity index 100%rename from app-sync.jsrename to src/app-sync.jsdiff --git a/app-sync.test.js b/src/app-sync.test.jssimilarity index 100%rename from app-sync.test.jsrename to src/app-sync.test.jsdiff --git a/src/app.js b/src/app.jsnew file mode 100644index 0000000000..f836e2fd5a--- /dev/null+++ b/src/app.js@@ -0,0 +1,72 @@+const fs = require('fs');+const express = require('express');+const actuator = require('express-actuator');+const bodyParser = require('body-parser');+const cors = require('cors');+const config = require('./load-config');++const accountApp = require('./app-account');+const syncApp = require('./app-sync');++const app = express();++process.on('unhandledRejection', (reason) => {+ console.log('Rejection:', reason);+});++app.use(cors());+app.use(bodyParser.json({ limit: '20mb' }));+app.use(bodyParser.raw({ type: 'application/actual-sync', limit: '20mb' }));+app.use(bodyParser.raw({ type: 'application/encrypted-file', limit: '50mb' }));++app.use('/sync', syncApp.handlers);+app.use('/account', accountApp.handlers);++app.get('/mode', (req, res) => {+ res.send(config.mode);+});++app.use(actuator()); // Provides /health, /metrics, /info++// The web frontend+app.use((req, res, next) => {+ res.set('Cross-Origin-Opener-Policy', 'same-origin');+ res.set('Cross-Origin-Embedder-Policy', 'require-corp');+ next();+});+app.use(express.static(config.webRoot, { index: false }));++app.get('/*', (req, res) => res.sendFile(config.webRoot + '/index.html'));++function parseHTTPSConfig(value) {+ if (value.startsWith('-----BEGIN')) {+ return value;+ }+ return fs.readFileSync(value);+}++module.exports = async function run() {+ if (!fs.existsSync(config.serverFiles)) {+ fs.mkdirSync(config.serverFiles);+ }++ if (!fs.existsSync(config.userFiles)) {+ fs.mkdirSync(config.userFiles);+ }++ await accountApp.init();+ await syncApp.init();++ if (config.https) {+ const https = require('https');+ const httpsOptions = {+ ...config.https,+ key: parseHTTPSConfig(config.https.key),+ cert: parseHTTPSConfig(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 + '...');+};diff --git a/config-types.ts b/src/config-types.tssimilarity index 100%rename from config-types.tsrename to src/config-types.tsdiff --git a/db.js b/src/db.jssimilarity index 100%rename from db.jsrename to src/db.jsdiff --git a/load-config.js b/src/load-config.jssimilarity index 80%rename from load-config.jsrename to src/load-config.jsindex 68159591ec..8b7b3d9b60 100644--- a/load-config.js+++ b/src/load-config.js@@ -1,7 +1,7 @@let userConfig;let fs = require('fs');-let { join } = require('path');-let root = fs.existsSync('/data') ? '/data' : __dirname;+let { join, dirname } = require('path');+let root = fs.existsSync('/data') ? '/data' : dirname(__dirname);if (process.env.ACTUAL_CONFIG_PATH) {userConfig = require(process.env.ACTUAL_CONFIG_PATH);@@ -18,7 +18,13 @@ if (process.env.ACTUAL_CONFIG_PATH) {let defaultConfig = {port: 5006,hostname: '::',- webRoot: join(__dirname, 'node_modules', '@actual-app', 'web', 'build')+ webRoot: join(+ dirname(__dirname),+ 'node_modules',+ '@actual-app',+ 'web',+ 'build'+ )};/** @type {import('./config-types').Config} */@@ -26,8 +32,8 @@ let config;if (process.env.NODE_ENV === 'test') {config = {mode: 'test',- serverFiles: join(__dirname, 'test-server-files'),- userFiles: join(__dirname, 'test-user-files'),+ serverFiles: join(dirname(__dirname), 'test-server-files'),+ userFiles: join(dirname(__dirname), 'test-user-files'),...defaultConfig};} else {diff --git a/sync-simple.js b/src/sync-simple.jssimilarity index 96%rename from sync-simple.jsrename to src/sync-simple.jsindex d87d414b86..74b0af2cc6 100644--- a/sync-simple.js+++ b/src/sync-simple.js@@ -15,7 +15,7 @@ function getGroupDb(groupId) {let db = openDatabase(path);if (needsInit) {- let sql = readFileSync(join(__dirname, 'sql/messages.sql'), 'utf8');+ let sql = readFileSync(join(__dirname, '../sql/messages.sql'), 'utf8');db.exec(sql);}diff --git a/util/error-middleware.js b/src/util/error-middleware.jssimilarity index 100%rename from util/error-middleware.jsrename to src/util/error-middleware.jsdiff --git a/util/paths.js b/src/util/paths.jssimilarity index 100%rename from util/paths.jsrename to src/util/paths.jsdiff --git a/util/validate-user.js b/src/util/validate-user.jssimilarity index 100%rename from util/validate-user.jsrename to src/util/validate-user.jsdiff --git a/util/async.js b/util/async.jsdeleted file mode 100644index 5acc6c1b08..0000000000--- a/util/async.js+++ /dev/null@@ -1,44 +0,0 @@-function sequential(fn) {- let sequenceState = {- running: null,- queue: []- };-- function pump() {- if (sequenceState.queue.length > 0) {- const next = sequenceState.queue.shift();- run(next.args, next.resolve, next.reject);- } else {- sequenceState.running = null;- }- }-- function run(args, resolve, reject) {- sequenceState.running = fn(...args);-- sequenceState.running.then(- (val) => {- pump();- resolve(val);- },- (err) => {- pump();- reject(err);- }- );- }-- return (...args) => {- if (!sequenceState.running) {- return new Promise((resolve, reject) => {- return run(args, resolve, reject);- });- } else {- return new Promise((resolve, reject) => {- sequenceState.queue.push({ resolve, reject, args });- });- }- };-}--module.exports = { sequential };