| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | #!/usr/bin/env node | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 19:16:36 -04:00
										 |  |  | process.on('unhandledRejection', error => { | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  |     // this makes sure that stacktrace of failed promise is printed out | 
					
						
							| 
									
										
										
										
											2017-10-26 19:16:36 -04:00
										 |  |  |     console.log(error); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // but also try to log it into file | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  |     require('./services/log').info(error); | 
					
						
							| 
									
										
										
										
											2017-10-22 22:56:42 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:33:03 +03:00
										 |  |  | process.on('SIGINT', function() { | 
					
						
							|  |  |  | 	console.log("Caught interrupt signal. Exiting."); | 
					
						
							|  |  |  | 	process.exit(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  | const { app, sessionParser } = require('./app'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const debug = require('debug')('node:server'); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const http = require('http'); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | const https = require('https'); | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  | const config = require('./services/config'); | 
					
						
							|  |  |  | const log = require('./services/log'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const appInfo = require('./services/app_info'); | 
					
						
							|  |  |  | const messagingService = require('./services/messaging'); | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  | const utils = require('./services/utils'); | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  | const sqlInit = require('./services/sql_init'); | 
					
						
							|  |  |  | const port = require('./services/port'); | 
					
						
							| 
									
										
										
										
											2019-01-26 19:59:51 +01:00
										 |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | let httpServer; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  | async function startTrilium() { | 
					
						
							|  |  |  |     const usedPort = await port; | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     app.set('port', usedPort); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:06:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     if (config['Network']['https']) { | 
					
						
							| 
									
										
										
										
											2019-04-08 20:08:29 +02: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"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |         const options = { | 
					
						
							|  |  |  |             key: fs.readFileSync(config['Network']['keyPath']), | 
					
						
							|  |  |  |             cert: fs.readFileSync(config['Network']['certPath']) | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-11-21 00:25:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |         httpServer = https.createServer(options, app); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |         log.info("App HTTPS server starting up at port " + usedPort); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         httpServer = http.createServer(app); | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |         log.info("App HTTP server starting up at port " + usedPort); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     log.info(JSON.stringify(appInfo, null, 2)); | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Listen on provided port, on all network interfaces. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     httpServer.keepAliveTimeout = 120000 * 5; | 
					
						
							|  |  |  |     httpServer.listen(usedPort); | 
					
						
							|  |  |  |     httpServer.on('error', onError); | 
					
						
							|  |  |  |     httpServer.on('listening', () => debug('Listening on port' + httpServer.address().port)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     sqlInit.dbReady.then(() => messagingService.init(httpServer, sessionParser)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |     if (utils.isElectron()) { | 
					
						
							|  |  |  |         const electronRouting = require('./routes/electron'); | 
					
						
							|  |  |  |         electronRouting(app); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  | startTrilium(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Event listener for HTTP server "error" event. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function onError(error) { | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     if (error.syscall !== 'listen') { | 
					
						
							|  |  |  |         throw error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // handle specific listen errors with friendly messages | 
					
						
							|  |  |  |     switch (error.code) { | 
					
						
							|  |  |  |         case 'EACCES': | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |             console.error('Port requires elevated privileges'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |             process.exit(1); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case 'EADDRINUSE': | 
					
						
							| 
									
										
										
										
											2018-07-31 19:50:18 +02:00
										 |  |  |             console.error('Port is already in use'); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |             process.exit(1); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } |