| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | const app_info = require('./services/app_info'); | 
					
						
							|  |  |  | const messaging = require('./services/messaging'); | 
					
						
							|  |  |  | const utils = require('./services/utils'); | 
					
						
							|  |  |  | const sql = require('./services/sql'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 23:06:16 -05:00
										 |  |  | const port = normalizePort(config['Network']['port'] || '3000'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | app.set('port', port); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Create HTTP server. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | let httpServer; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | if (config['Network']['https']) { | 
					
						
							|  |  |  |     const options = { | 
					
						
							|  |  |  |         key: fs.readFileSync(config['Network']['keyPath']), | 
					
						
							|  |  |  |         cert: fs.readFileSync(config['Network']['certPath']) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     httpServer = https.createServer(options, app); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     log.info("App HTTPS server starting up at port " + port); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else { | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     httpServer = http.createServer(app); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:55:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     log.info("App HTTP server starting up at port " + port); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-16 23:06:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 00:25:53 -05:00
										 |  |  | log.info(JSON.stringify(app_info, null, 2)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Listen on provided port, on all network interfaces. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 00:02:13 -05:00
										 |  |  | httpServer.keepAliveTimeout = 120000 * 5; | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | httpServer.listen(port); | 
					
						
							|  |  |  | httpServer.on('error', onError); | 
					
						
							|  |  |  | httpServer.on('listening', onListening); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 19:18:33 -05:00
										 |  |  | sql.dbReady.then(() => messaging.init(httpServer, sessionParser)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  | if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2018-01-28 22:18:14 -05:00
										 |  |  |     const electronRouting = require('./routes/electron'); | 
					
						
							| 
									
										
										
										
											2017-11-30 20:51:35 -05:00
										 |  |  |     electronRouting(app); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Normalize a port into a number, string, or false. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function normalizePort(val) { | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     const port = parseInt(val, 10); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     if (isNaN(port)) { | 
					
						
							|  |  |  |         // named pipe | 
					
						
							|  |  |  |         return val; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     if (port >= 0) { | 
					
						
							|  |  |  |         // port number | 
					
						
							|  |  |  |         return port; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     return false; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const bind = typeof port === 'string' | 
					
						
							|  |  |  |         ? 'Pipe ' + port | 
					
						
							|  |  |  |         : 'Port ' + port; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // handle specific listen errors with friendly messages | 
					
						
							|  |  |  |     switch (error.code) { | 
					
						
							|  |  |  |         case 'EACCES': | 
					
						
							|  |  |  |             console.error(bind + ' requires elevated privileges'); | 
					
						
							|  |  |  |             process.exit(1); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case 'EADDRINUSE': | 
					
						
							|  |  |  |             console.error(bind + ' is already in use'); | 
					
						
							|  |  |  |             process.exit(1); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             throw error; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Event listener for HTTP server "listening" event. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function onListening() { | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     const addr = httpServer.address(); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     const bind = typeof addr === 'string' | 
					
						
							|  |  |  |         ? 'pipe ' + addr | 
					
						
							|  |  |  |         : 'port ' + addr.port; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug('Listening on ' + bind); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } |