2017-11-07 08:23:35 +08:00
const log = require ( './services/log' ) ;
2017-10-15 11:31:44 +08:00
const express = require ( 'express' ) ;
const path = require ( 'path' ) ;
const favicon = require ( 'serve-favicon' ) ;
const cookieParser = require ( 'cookie-parser' ) ;
const bodyParser = require ( 'body-parser' ) ;
2017-10-16 04:32:49 +08:00
const helmet = require ( 'helmet' ) ;
const session = require ( 'express-session' ) ;
2017-10-16 05:07:34 +08:00
const FileStore = require ( 'session-file-store' ) ( session ) ;
2017-10-24 10:36:18 +08:00
const os = require ( 'os' ) ;
2017-10-24 11:38:52 +08:00
const sessionSecret = require ( './services/session_secret' ) ;
2018-03-29 11:41:22 +08:00
const cls = require ( './services/cls' ) ;
2018-03-31 22:51:37 +08:00
require ( './entities/entity_constructor' ) ;
2018-08-01 15:26:02 +08:00
require ( './services/handlers' ) ;
2017-10-15 11:31:44 +08:00
const app = express ( ) ;
// view engine setup
app . set ( 'views' , path . join ( _ _dirname , 'views' ) ) ;
app . set ( 'view engine' , 'ejs' ) ;
2018-10-14 17:31:23 +08:00
app . use ( helmet ( {
hidePoweredBy : false // deactivated because electron 4.0 crashes on this right after startup
} ) ) ;
2017-10-25 10:04:52 +08:00
app . use ( ( req , res , next ) => {
log . request ( req ) ;
next ( ) ;
} ) ;
2018-03-29 11:41:22 +08:00
app . use ( ( req , res , next ) => {
cls . namespace . bindEmitter ( req ) ;
cls . namespace . bindEmitter ( res ) ;
cls . init ( ( ) => {
cls . namespace . set ( "Hi" ) ;
next ( ) ;
} ) ;
} ) ;
2019-01-30 04:19:08 +08:00
app . use ( bodyParser . json ( { limit : '500mb' } ) ) ;
2017-10-15 11:31:44 +08:00
app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
app . use ( cookieParser ( ) ) ;
2017-10-16 07:47:05 +08:00
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
2019-03-30 16:39:58 +08:00
app . use ( '/libraries' , express . static ( path . join ( _ _dirname , '..' , 'libraries' ) ) ) ;
app . use ( '/images' , express . static ( path . join ( _ _dirname , '..' , 'images' ) ) ) ;
2017-12-01 12:50:42 +08:00
const sessionParser = session ( {
2017-10-24 11:38:52 +08:00
secret : sessionSecret ,
2017-10-16 04:32:49 +08: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-12-01 12:50:42 +08:00
// path: "/",
2017-10-16 04:32:49 +08:00
httpOnly : true ,
2018-07-24 14:12:36 +08:00
maxAge : 24 * 60 * 60 * 1000 // in milliseconds
2017-10-16 05:07:34 +08:00
} ,
store : new FileStore ( {
2017-10-17 07:14:15 +08:00
ttl : 30 * 24 * 3600 ,
2017-10-24 10:36:18 +08:00
path : os . tmpdir ( ) + '/trilium-sessions'
2017-10-16 05:07:34 +08:00
} )
2017-12-01 12:50:42 +08:00
} ) ;
app . use ( sessionParser ) ;
2017-10-21 12:19:13 +08:00
2019-03-30 16:39:58 +08:00
app . use ( favicon ( _ _dirname + '/../images/app-icons/win/icon.ico' ) ) ;
2017-10-15 11:31:44 +08:00
2017-11-04 11:00:35 +08:00
require ( './routes/routes' ) . register ( app ) ;
2017-10-15 11:31:44 +08:00
2019-01-27 19:28:20 +08:00
require ( './routes/custom' ) . register ( app ) ;
2019-05-30 05:13:15 +08: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-15 11:31:44 +08:00
// catch 404 and forward to error handler
2017-10-25 10:04:52 +08:00
app . use ( ( req , res , next ) => {
2017-10-30 02:55:48 +08:00
const err = new Error ( 'Router not found for request ' + req . url ) ;
2017-10-15 11:31:44 +08:00
err . status = 404 ;
next ( err ) ;
} ) ;
// error handler
2017-10-25 10:04:52 +08:00
app . use ( ( err , req , res , next ) => {
2019-06-13 03:44:33 +08:00
if ( err && err . message && err . message . includes ( "Invalid package" ) ) {
// electron 6 outputs a lot of such errors which do not seem important
}
2019-06-16 15:15:37 +08:00
else {
log . info ( err ) ;
}
2017-10-29 01:19:12 +08:00
res . status ( err . status || 500 ) ;
res . send ( {
message : err . message
} ) ;
2017-10-15 11:31:44 +08:00
} ) ;
2017-10-29 00:23:11 +08:00
// triggers sync timer
2017-10-22 09:10:33 +08:00
require ( './services/sync' ) ;
2017-10-29 00:23:11 +08:00
// triggers backup timer
require ( './services/backup' ) ;
2017-12-15 11:16:26 +08:00
// trigger consistency checks timer
require ( './services/consistency_checks' ) ;
2018-03-03 09:56:58 +08:00
require ( './services/scheduler' ) ;
2017-12-01 12:50:42 +08:00
module . exports = {
app ,
sessionParser
} ;