trilium/src/www

110 lines
3.2 KiB
Text
Raw Normal View History

#!/usr/bin/env node
process.on('unhandledRejection', error => {
2017-10-23 10:56:42 +08:00
// this makes sure that stacktrace of failed promise is printed out
console.log(error);
2017-10-26 10:39:21 +08:00
// but also try to log it into file
2018-01-29 11:18:14 +08:00
require('./services/log').info(error);
2017-10-23 10:56:42 +08:00
});
process.on('SIGINT', function() {
console.log("Caught interrupt signal. Exiting.");
process.exit();
});
2018-01-29 11:18:14 +08:00
const { app, sessionParser } = require('./app');
2017-10-26 10:39:21 +08:00
const debug = require('debug')('node:server');
2017-11-17 12:55:50 +08:00
const fs = require('fs');
2017-10-26 10:39:21 +08:00
const http = require('http');
2017-11-17 12:55:50 +08:00
const https = require('https');
2018-01-29 11:18:14 +08:00
const config = require('./services/config');
const log = require('./services/log');
const appInfo = require('./services/app_info');
2019-08-27 02:21:43 +08:00
const ws = require('./services/ws.js');
2018-01-29 11:18:14 +08:00
const utils = require('./services/utils');
const sqlInit = require('./services/sql_init');
const port = require('./services/port');
const host = require('./services/host');
const semver = require('semver');
if (!semver.satisfies(process.version, ">=10.5.0")) {
console.error("Trilium only supports node.js 10.5 and later");
process.exit(1);
}
let httpServer;
async function startTrilium() {
const usedPort = await port;
const usedHost = await host;
2017-11-17 12:55:50 +08:00
app.set('port', usedPort);
app.set('host', usedHost);
if (config['Network']['https']) {
2019-04-09 02:08:29 +08:00
if (!config['Network']['keyPath'] || !config['Network']['keyPath'].trim().length) {
throw new Error("keyPath in config.ini is required when https=true, but it's empty");
}
if (!config['Network']['certPath'] || !config['Network']['certPath'].trim().length) {
throw new Error("certPath in config.ini is required when https=true, but it's empty");
}
const options = {
key: fs.readFileSync(config['Network']['keyPath']),
cert: fs.readFileSync(config['Network']['certPath'])
};
2017-11-21 13:25:53 +08:00
httpServer = https.createServer(options, app);
log.info("App HTTPS server starting up at port " + usedPort);
}
else {
httpServer = http.createServer(app);
log.info("App HTTP server starting up at port " + usedPort);
}
log.info(JSON.stringify(appInfo, null, 2));
/**
* Listen on provided port, on all network interfaces.
*/
httpServer.keepAliveTimeout = 120000 * 5;
httpServer.listen(usedPort, usedHost);
2019-12-05 05:59:26 +08:00
httpServer.on('error', error => {
if (error.syscall !== 'listen') {
throw error;
}
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(`Port ${usedPort} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`Port ${usedPort} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
);
httpServer.on('listening', () => debug('Listening on port' + httpServer.address().port));
2019-08-27 02:21:43 +08:00
sqlInit.dbReady.then(() => ws.init(httpServer, sessionParser));
if (utils.isElectron()) {
const electronRouting = require('./routes/electron');
electronRouting(app);
2017-10-26 10:39:21 +08:00
}
}
startTrilium();