actual-server/app.js

66 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2022-04-29 15:10:20 +00:00
const fs = require('fs');
2022-03-31 17:19:08 +00:00
const express = require('express');
2022-05-01 16:07:45 +00:00
const actuator = require('express-actuator');
2022-03-31 17:19:08 +00:00
const bodyParser = require('body-parser');
const cors = require('cors');
2022-04-29 02:09:40 +00:00
const config = require('./load-config');
2022-03-31 17:19:08 +00:00
const accountApp = require('./app-account');
const syncApp = require('./app-sync');
const app = express();
2022-05-20 21:37:42 +00:00
process.on('unhandledRejection', (reason) => {
2022-03-31 17:19:08 +00:00
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);
2022-04-29 02:09:40 +00:00
app.get('/mode', (req, res) => {
2022-03-31 17:19:08 +00:00
res.send(config.mode);
});
2022-05-01 16:07:45 +00:00
app.use(actuator()); // Provides /health, /metrics, /info
2022-04-29 02:09:40 +00:00
// The web frontend
2022-05-20 02:42:56 +00:00
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(__dirname + '/node_modules/@actual-app/web/build', {
index: false
})
);
2022-04-29 02:09:40 +00:00
app.get('/*', (req, res) => {
res.sendFile(__dirname + '/node_modules/@actual-app/web/build/index.html');
});
2022-03-31 17:19:08 +00:00
async function run() {
2022-04-29 15:10:20 +00:00
if (!fs.existsSync(config.serverFiles)) {
fs.mkdirSync(config.serverFiles);
}
if (!fs.existsSync(config.userFiles)) {
fs.mkdirSync(config.userFiles);
}
2022-03-31 17:19:08 +00:00
await accountApp.init();
await syncApp.init();
2022-04-29 21:30:30 +00:00
console.log('Listening on ' + config.hostname + ':' + config.port + '...');
app.listen(config.port, config.hostname);
2022-03-31 17:19:08 +00:00
}
2022-05-20 21:37:42 +00:00
run().catch((err) => {
2022-03-31 17:19:08 +00:00
console.log('Error starting app:', err);
process.exit(1);
});