trilium/app.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

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);
const os = require('os');
2017-11-03 08:48:02 +08:00
const options = require('./services/options');
const log = require('./services/log');
const utils = require('./services/utils');
const dataDir = require('./services/data_dir');
2017-10-24 11:38:52 +08:00
const sessionSecret = require('./services/session_secret');
const db = require('sqlite');
db.open(dataDir.DOCUMENT_PATH, { Promise }).then(async () => {
2017-11-03 08:48:02 +08:00
if (!await options.getOption('document_id')) {
await options.setOption('document_id', utils.randomString(32));
}
2017-11-03 08:48:02 +08:00
if (!await options.getOption('document_secret')) {
await options.setOption('document_secret', utils.randomSecureToken(32));
}
});
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
2017-10-16 04:32:49 +08:00
app.use(helmet());
app.use((req, res, next) => {
log.request(req);
next();
});
app.use(bodyParser.json({limit: '50mb'}));
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')));
2017-10-16 04:32:49 +08:00
app.use(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: {
// path: "/",
httpOnly: true,
maxAge: 1800000
2017-10-16 05:07:34 +08:00
},
store: new FileStore({
2017-10-17 07:14:15 +08:00
ttl: 30 * 24 * 3600,
path: os.tmpdir() + '/trilium-sessions'
2017-10-16 05:07:34 +08:00
})
2017-10-16 04:32:49 +08:00
}));
2017-10-21 12:19:13 +08:00
app.use(favicon(__dirname + '/public/images/app-icons/favicon.ico'));
2017-11-04 11:00:35 +08:00
require('./routes/routes').register(app);
// catch 404 and forward to error handler
app.use((req, res, next) => {
2017-10-30 02:55:48 +08:00
const err = new Error('Router not found for request ' + req.url);
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res, next) => {
log.error(err.message);
res.status(err.status || 500);
res.send({
message: err.message
});
});
// triggers sync timer
2017-10-22 09:10:33 +08:00
require('./services/sync');
// triggers backup timer
require('./services/backup');
module.exports = app;