2017-11-06 19:23:35 -05:00
const log = require ( './services/log' ) ;
2017-10-14 23:31:44 -04:00
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' ) ;
2023-04-07 19:51:34 +08:00
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' ) ;
2020-05-24 00:21:20 +02:00
const dataDir = require ( './services/data_dir' ) ;
2021-11-18 21:35:23 +01:00
const utils = require ( './services/utils' ) ;
2022-10-26 23:50:54 +02:00
const assetPath = require ( './services/asset_path' ) ;
2023-04-07 19:51:34 +08:00
const env = require ( './services/env' ) ;
2018-08-01 09:26:02 +02:00
require ( './services/handlers' ) ;
2022-01-10 17:09:20 +01:00
require ( './becca/becca_loader' ) ;
2017-10-14 23:31:44 -04:00
const app = express ( ) ;
// view engine setup
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'ejs' ) ;
2022-08-22 19:29:58 +02:00
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
} ) ) ;
2017-10-24 22:04:52 -04:00
2023-04-07 19:51:34 +08: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' } ) ) ;
2022-02-18 22:34:46 +01:00
app . use ( express . raw ( { limit : '500mb' } ) ) ;
2022-01-12 19:32:23 +01:00
app . use ( express . urlencoded ( { extended : false } ) ) ;
2017-10-14 23:31:44 -04:00
app . use ( cookieParser ( ) ) ;
2022-10-26 23:50:54 +02:00
app . use ( express . static ( path . join ( _ _dirname , 'public/root' ) ) ) ;
2023-04-07 19:51:34 +08:00
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' ) ) ) ;
2022-10-27 20:34:53 +02:00
app . use ( ` /assets/vX/fonts ` , express . static ( path . join ( _ _dirname , 'public/fonts' ) ) ) ;
2023-04-07 19:51:34 +08:00
app . use ( ` / ${ assetPath } /stylesheets ` , persistentCacheStatic ( path . join ( _ _dirname , 'public/stylesheets' ) ) ) ;
2022-10-27 20:34:53 +02:00
app . use ( ` /assets/vX/stylesheets ` , express . static ( path . join ( _ _dirname , 'public/stylesheets' ) ) ) ;
2023-04-07 19:51:34 +08:00
app . use ( ` / ${ assetPath } /libraries ` , persistentCacheStatic ( path . join ( _ _dirname , '..' , 'libraries' ) ) ) ;
2022-10-27 20:34:53 +02:00
app . use ( ` /assets/vX/libraries ` , express . static ( path . join ( _ _dirname , '..' , 'libraries' ) ) ) ;
2022-05-09 16:08:56 +02:00
// excalidraw-view mode in shared notes
2023-04-07 19:51:34 +08:00
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' ) ) ) ;
2022-05-09 16:08:56 +02:00
// expose whole dist folder since complete assets are needed in edit and share
2022-10-26 23:50:54 +02:00
app . use ( ` /node_modules/@excalidraw/excalidraw/dist/ ` , express . static ( path . join ( _ _dirname , '..' , 'node_modules/@excalidraw/excalidraw/dist/' ) ) ) ;
2023-04-07 19:51:34 +08:00
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' ) ) ) ;
2022-10-27 20:34:53 +02:00
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' ) ) ) ;
2017-11-30 23:50:42 -05:00
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 : {
2017-11-30 23:50:42 -05:00
// path: "/",
2017-10-15 16:32:49 -04:00
httpOnly : true ,
2023-04-07 19:51:34 +08:00
maxAge : 24 * 60 * 60 * 1000 // in milliseconds
2017-10-15 17:07:34 -04:00
} ,
2020-12-20 22:20:21 +01: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 ,
2022-12-21 15:19:05 +01:00
path : ` ${ dataDir . TRILIUM _DATA _DIR } /sessions `
2017-10-15 17:07:34 -04:00
} )
2017-11-30 23:50:42 -05:00
} ) ;
app . use ( sessionParser ) ;
2017-10-21 00:19:13 -04:00
2022-12-21 15:19:05 +01:00
app . use ( favicon ( ` ${ _ _dirname } /../images/app-icons/win/icon.ico ` ) ) ;
2017-10-14 23:31:44 -04:00
2017-11-03 23:00:35 -04:00
require ( './routes/routes' ) . register ( app ) ;
2017-10-14 23:31:44 -04:00
2019-01-27 12:28:20 +01:00
require ( './routes/custom' ) . register ( app ) ;
2019-05-29 23:13:15 +02:00
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 ) ;
} ) ;
2017-10-14 23:31:44 -04:00
// catch 404 and forward to error handler
2017-10-24 22:04:52 -04:00
app . use ( ( req , res , next ) => {
2022-12-21 15:19:05 +01:00
const err = new Error ( ` Router not found for request ${ req . url } ` ) ;
2017-10-14 23:31:44 -04:00
err . status = 404 ;
next ( err ) ;
} ) ;
// error handler
2017-10-24 22:04:52 -04:00
app . use ( ( err , req , res , next ) => {
2019-10-06 21:35:26 +02:00
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" ) )
2019-10-06 21:35:26 +02:00
) ) {
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 ) ;
}
2017-10-28 13:19:12 -04:00
res . status ( err . status || 500 ) ;
res . send ( {
message : err . message
} ) ;
2017-10-14 23:31:44 -04:00
} ) ;
2017-10-28 12:23:11 -04:00
// triggers sync timer
2017-10-21 21:10:33 -04:00
require ( './services/sync' ) ;
2017-10-28 12:23:11 -04:00
// triggers backup timer
require ( './services/backup' ) ;
2017-12-14 22:16:26 -05:00
// trigger consistency checks timer
require ( './services/consistency_checks' ) ;
2018-03-02 20:56:58 -05:00
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
2017-11-30 23:50:42 -05:00
module . exports = {
app ,
sessionParser
2020-05-21 14:05:56 +02:00
} ;