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' ) ;
const bodyParser = require ( 'body-parser' ) ;
2017-10-15 16:32:49 -04:00
const helmet = require ( 'helmet' ) ;
const session = require ( 'express-session' ) ;
2017-10-15 17:07:34 -04:00
const FileStore = require ( 'session-file-store' ) ( session ) ;
2017-10-23 22:36:18 -04:00
const os = require ( 'os' ) ;
2017-11-02 20:48:02 -04:00
const options = require ( './services/options' ) ;
2017-10-24 22:04:52 -04:00
const log = require ( './services/log' ) ;
2017-10-28 19:55:55 -04:00
const utils = require ( './services/utils' ) ;
2017-11-05 17:58:55 -05:00
const sql = require ( './services/sql' ) ;
2017-10-14 23:31:44 -04:00
2017-10-23 23:30:23 -04:00
const dataDir = require ( './services/data_dir' ) ;
2017-10-23 23:38:52 -04:00
const sessionSecret = require ( './services/session_secret' ) ;
2017-10-14 23:31:44 -04:00
const db = require ( 'sqlite' ) ;
2017-10-28 19:55:55 -04:00
db . open ( dataDir . DOCUMENT _PATH , { Promise } ) . then ( async ( ) => {
2017-11-05 17:58:55 -05:00
const tableResults = await sql . getResults ( "SELECT name FROM sqlite_master WHERE type='table' AND name='notes'" ) ;
if ( tableResults . length !== 1 ) {
console . log ( "No connection to initialized DB." ) ;
process . exit ( 1 ) ;
}
2017-11-02 20:48:02 -04:00
if ( ! await options . getOption ( 'document_id' ) ) {
await options . setOption ( 'document_id' , utils . randomString ( 32 ) ) ;
2017-10-28 19:55:55 -04:00
}
2017-11-02 20:48:02 -04:00
if ( ! await options . getOption ( 'document_secret' ) ) {
await options . setOption ( 'document_secret' , utils . randomSecureToken ( 32 ) ) ;
2017-10-28 19:55:55 -04:00
}
2017-11-05 17:58:55 -05:00
} )
. catch ( e => {
console . log ( "Error connecting to DB." , e ) ;
process . exit ( 1 ) ;
} ) ;
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' ) ;
2017-10-15 16:32:49 -04:00
app . use ( helmet ( ) ) ;
2017-10-24 22:04:52 -04:00
app . use ( ( req , res , next ) => {
log . request ( req ) ;
next ( ) ;
} ) ;
2017-10-26 23:21:31 -04:00
app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
2017-10-14 23:31:44 -04:00
app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
app . use ( cookieParser ( ) ) ;
2017-10-15 19:47:05 -04:00
app . use ( express . static ( path . join ( _ _dirname , 'public' ) ) ) ;
2017-10-15 16:32:49 -04:00
app . use ( 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: "/",
httpOnly : true ,
maxAge : 1800000
2017-10-15 17:07:34 -04:00
} ,
store : new FileStore ( {
2017-10-16 19:14:15 -04:00
ttl : 30 * 24 * 3600 ,
2017-10-23 22:36:18 -04:00
path : os . tmpdir ( ) + '/trilium-sessions'
2017-10-15 17:07:34 -04:00
} )
2017-10-15 16:32:49 -04:00
} ) ) ;
2017-10-21 00:19:13 -04:00
app . use ( favicon ( _ _dirname + '/public/images/app-icons/favicon.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
// catch 404 and forward to error handler
2017-10-24 22:04:52 -04:00
app . use ( ( req , res , next ) => {
2017-10-29 14:55:48 -04: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 ) => {
log . error ( err . message ) ;
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-10-14 23:31:44 -04:00
module . exports = app ;