Notes/src/app.js

147 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-11-06 19:23:35 -05:00
const log = require('./services/log');
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
2017-10-15 16:32:49 -04:00
const helmet = require('helmet');
const session = require('express-session');
const compression = require('compression');
2017-10-15 17:07:34 -04:00
const FileStore = require('session-file-store')(session);
2017-10-23 23:38:52 -04:00
const sessionSecret = require('./services/session_secret');
const dataDir = require('./services/data_dir');
2021-11-18 21:35:23 +01:00
const utils = require('./services/utils');
const assetPath = require('./services/asset_path');
const env = require('./services/env');
require('./services/handlers');
2022-01-10 17:09:20 +01:00
require('./becca/becca_loader');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
if (!utils.isElectron()) {
app.use(compression()); // HTTP compression
}
2022-08-20 17:30:35 -04:00
2018-10-14 11:31:23 +02:00
app.use(helmet({
2022-01-12 19:32:23 +01:00
hidePoweredBy: false, // errors out in electron
2022-05-18 22:56:29 +02:00
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false
2018-10-14 11:31:23 +02:00
}));
const persistentCacheStatic = (root, options) => {
if (!env.isDev()) {
options = {
maxAge: '1y',
...options
};
}
return express.static(root, options);
};
2022-01-12 19:32:23 +01:00
app.use(express.text({limit: '500mb'}));
app.use(express.json({limit: '500mb'}));
app.use(express.raw({limit: '500mb'}));
2022-01-12 19:32:23 +01:00
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public/root')));
app.use(`/${assetPath}/app`, persistentCacheStatic(path.join(__dirname, 'public/app')));
app.use(`/${assetPath}/app-dist`, persistentCacheStatic(path.join(__dirname, 'public/app-dist')));
app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(__dirname, 'public/fonts')));
app.use(`/assets/vX/fonts`, express.static(path.join(__dirname, 'public/fonts')));
app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(__dirname, 'public/stylesheets')));
app.use(`/assets/vX/stylesheets`, express.static(path.join(__dirname, 'public/stylesheets')));
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(__dirname, '..', 'libraries')));
app.use(`/assets/vX/libraries`, express.static(path.join(__dirname, '..', 'libraries')));
// excalidraw-view mode in shared notes
app.use(`/${assetPath}/node_modules/react/umd/react.production.min.js`, persistentCacheStatic(path.join(__dirname, '..', 'node_modules/react/umd/react.production.min.js')));
app.use(`/${assetPath}/node_modules/react-dom/umd/react-dom.production.min.js`, persistentCacheStatic(path.join(__dirname, '..', 'node_modules/react-dom/umd/react-dom.production.min.js')));
// expose whole dist folder since complete assets are needed in edit and share
app.use(`/node_modules/@excalidraw/excalidraw/dist/`, express.static(path.join(__dirname, '..', 'node_modules/@excalidraw/excalidraw/dist/')));
app.use(`/${assetPath}/node_modules/@excalidraw/excalidraw/dist/`, persistentCacheStatic(path.join(__dirname, '..', 'node_modules/@excalidraw/excalidraw/dist/')));
app.use(`/${assetPath}/images`, persistentCacheStatic(path.join(__dirname, '..', 'images')));
app.use(`/assets/vX/images`, express.static(path.join(__dirname, '..', 'images')));
2022-12-02 22:06:18 +01:00
app.use(`/manifest.webmanifest`, express.static(path.join(__dirname, 'public/manifest.webmanifest')));
app.use(`/robots.txt`, express.static(path.join(__dirname, 'public/robots.txt')));
const sessionParser = session({
2017-10-23 23:38:52 -04:00
secret: sessionSecret,
2017-10-15 16:32:49 -04:00
resave: false, // true forces the session to be saved back to the session store, even if the session was never modified during the request.
saveUninitialized: false, // true forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified.
cookie: {
// path: "/",
2017-10-15 16:32:49 -04:00
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000 // in milliseconds
2017-10-15 17:07:34 -04:00
},
name: 'trilium.sid',
2017-10-15 17:07:34 -04:00
store: new FileStore({
2017-10-16 19:14:15 -04:00
ttl: 30 * 24 * 3600,
path: `${dataDir.TRILIUM_DATA_DIR}/sessions`
2017-10-15 17:07:34 -04:00
})
});
app.use(sessionParser);
2017-10-21 00:19:13 -04:00
app.use(favicon(`${__dirname}/../images/app-icons/win/icon.ico`));
2017-11-03 23:00:35 -04:00
require('./routes/routes').register(app);
require('./routes/custom').register(app);
app.use((err, req, res, next) => {
if (err.code !== 'EBADCSRFTOKEN') {
return next(err);
}
log.error(`Invalid CSRF token: ${req.headers['x-csrf-token']}, secret: ${req.cookies['_csrf']}`);
err = new Error('Invalid CSRF token');
err.status = 403;
next(err);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error(`Router not found for request ${req.url}`);
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res, next) => {
if (err && err.message && (
2020-07-02 23:15:37 +02:00
(err.message.includes("Router not found for request") && err.message.includes(".js.map"))
2019-10-28 20:26:40 +01:00
|| (err.message.includes("Router not found for request") && err.message.includes(".css.map"))
)) {
2020-07-02 23:15:37 +02:00
// ignore
2019-06-12 21:44:33 +02:00
}
2019-06-16 09:15:37 +02:00
else {
log.info(err);
}
res.status(err.status || 500);
res.send({
message: err.message
});
});
// triggers sync timer
2017-10-21 21:10:33 -04:00
require('./services/sync');
// triggers backup timer
require('./services/backup');
2017-12-14 22:16:26 -05:00
// trigger consistency checks timer
require('./services/consistency_checks');
require('./services/scheduler');
2021-11-18 21:35:23 +01:00
if (utils.isElectron()) {
require('@electron/remote/main').initialize();
}
2021-11-16 22:43:08 +01:00
module.exports = {
app,
sessionParser
};