From 985819c31ba8151be9f572a955b8e6e31432911e Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 28 Aug 2017 12:37:46 -0400 Subject: [PATCH] [BC BREAK] bin/server.js and associated changes This is based (very) loosely on an internal script to start the daemon. It accepts a configuration and starts the daemon. To accommodate the configuration, a number of miscellaneous changes have been made. The vanilla configuration shows the concept, but it has not yet been fully implemented; that'll likely happen at a later date. Until then, the existing environment-variable-based configuration will be used. * bin/server.js: Add file. * conf/vanilla-server.json: Example configuration added. * src/server/daemon/Daemon.js (_httpPort): Remove field. (_conf): Add field. (__construct): [BC BREAK] Accept conf instead of port and log priority. Move initialization code into `start'. (start): [BC BREAK] Initialization code moved here. Now returns promise for entire daemon, which will error in the event of an error starting. Move existing code into `_startDaemon'. (_startDaemon): Old `start' code. Invoked after `start' initialization. (_createDebugLog, _createAccessLog): Use configuration. Return promise. (_initHttpServer): Use configuration. (_httpError): Add function to output error and exit. Extracted from `_initHttpServer'. * src/server/daemon/scripts.js: [BC BREAK] Append "program/" to `LV_LEGACY_PATH' so that it can be re-used for script lookups rather than using the cwd. This removes the need of the cwd being the legacy src path. --- bin/server.js | 66 +++++++++++++++++++ conf/vanilla-server.json | 55 ++++++++++++++++ src/server/daemon/Daemon.js | 122 +++++++++++++++++++++-------------- src/server/daemon/scripts.js | 7 +- 4 files changed, 200 insertions(+), 50 deletions(-) create mode 100644 bin/server.js create mode 100644 conf/vanilla-server.json diff --git a/bin/server.js b/bin/server.js new file mode 100644 index 0000000..c1e9f9c --- /dev/null +++ b/bin/server.js @@ -0,0 +1,66 @@ +/** + * Start the Liza Server + * + * Copyright (C) 2017 R-T Specialty, LLC. + * + * This file is part of the Liza Data Collection Framework. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +'use strict'; + +const fs = require( 'fs' ); + +const { + conf: { + ConfLoader, + ConfStore, + }, + server, + version, +} = require( '../' ); + +// kluge for now +const conf_path = ( + ( process.argv[ 2 ] === '-c' ) + ? process.argv[ 3 ] + : '' +) || __dirname + '/../conf/vanilla-server.json'; + + +ConfLoader( fs, ConfStore ) + .fromFile( conf_path ) + .then( conf => Promise.all( [ + conf.get( 'name' ), + conf.get( 'daemon' ), + Promise.resolve( conf ), + ] ) ) + .then( ([ name, daemon, conf ]) => + { + greet( name, daemon ); + return server.daemon[ daemon ]( conf ).start(); + } ) + .catch( e => { + console.error( e.stack ); + process.exit( 1 ); + } ); + + +function greet( name, daemon ) +{ + console.log( `${name} (liza-${version})`); + console.log( `Server configuration: ${conf_path}` ); + console.log( `Starting with ${daemon}, pid ${process.pid}` ); +} diff --git a/conf/vanilla-server.json b/conf/vanilla-server.json new file mode 100644 index 0000000..e056466 --- /dev/null +++ b/conf/vanilla-server.json @@ -0,0 +1,55 @@ +{ + "name": "Liza Server", + "daemon": "DevDaemon", + + "http": { + "port": 8822 + }, + + "log": { + "priority": 10, + "access": { + "path": "/var/log/node/access.log" + }, + "debug": { + "path": "/var/log/node/debug.log" + } + }, + + "user": { + "session": { + "handler": { + "type": "php", + "cookie": "PHPSESSID" + }, + "store": { + "type": "memcache", + "host": "localhost", + "port": 11211 + } + } + }, + + "documentStore": { + "store": "mongodb", + "host": "localhost", + "port": 27017 + }, + + "services": { + "rating": { + "process": { + "port": 5859, + "argv": "inherit" + }, + "remote": { + "host": "localhost", + "domain": "" + } + }, + "c1export": { + "host": "localhost", + "domain": "" + } + } +} diff --git a/src/server/daemon/Daemon.js b/src/server/daemon/Daemon.js index f361a22..700af97 100644 --- a/src/server/daemon/Daemon.js +++ b/src/server/daemon/Daemon.js @@ -34,10 +34,10 @@ var AbstractClass = require( 'easejs' ).AbstractClass, module.exports = AbstractClass( 'Daemon', { /** - * Quote server port - * @type {number} + * System configuration + * @type {Store} */ - 'private _httpPort': 0, + 'private _conf': null, /** * Server to accept HTTP requests @@ -96,17 +96,9 @@ module.exports = AbstractClass( 'Daemon', 'private _rater': null, - 'public __construct': function( http_port, log_priority ) + 'public __construct': function( conf ) { - this._httpPort = http_port; - - this._rater = liza.server.rater.ProcessManager(); - this._httpServer = this.getHttpServer(); - this._accessLog = this._createAccessLog(); - this._debugLog = this._createDebugLog( log_priority ); - this._encService = this.getEncryptionService(); - this._memcache = this.getMemcacheClient(); - this._routers = this.getRouters(); + this._conf = conf; }, @@ -115,10 +107,28 @@ module.exports = AbstractClass( 'Daemon', * * @return {undefined} */ - 'public start': function() + 'public start'() { - var _self = this; + return Promise.all( [ + this._createDebugLog(), + this._createAccessLog(), + ] ).then( ([ debug_log, access_log ]) => + { + this._debugLog = debug_log; + this._accessLog = access_log; + this._httpServer = this.getHttpServer(); + this._rater = liza.server.rater.ProcessManager(); + this._encService = this.getEncryptionService(); + this._memcache = this.getMemcacheClient(); + this._routers = this.getRouters(); + } ) + .then( () => this._startDaemon() ); + }, + + + 'private _startDaemon'() + { this._debugLog.log( this._debugLog.PRIORITY_IMPORTANT, "Access log path: %s", this._accessLogPath ); @@ -128,18 +138,18 @@ module.exports = AbstractClass( 'Daemon', ); this._initSignalHandlers(); - this._testEncryptionService( function() + this._testEncryptionService( () => { - _self._memcacheConnect(); - _self._initMemoryLogger(); + this._memcacheConnect(); + this._initMemoryLogger(); - _self._initRouters(); - _self._initHttpServer( function() + this._initRouters(); + this._initHttpServer( () => { - _self._initUncaughtExceptionHandler(); + this._initUncaughtExceptionHandler(); // ready to roll - _self._debugLog.log( _self._debugLog.PRIORITY_INFO, + this._debugLog.log( this._debugLog.PRIORITY_INFO, "Daemon initialization complete." ); } ); @@ -299,22 +309,30 @@ module.exports = AbstractClass( 'Daemon', 'private _createAccessLog': function() { - this._accessLogPath = - ( process.env.LOG_PATH_ACCESS || '/var/log/node/access.log' ); - - return this.getAccessLog()( this._accessLogPath ); + return this._conf.get( 'log.access.path' ) + .then( log_path => + { + this._accessLogPath = log_path; + return this.getAccessLog()( this._accessLogPath ); + } ); }, - 'private _createDebugLog': function( log_priority ) + 'private _createDebugLog': function() { - this._debugLogPath = - ( process.env.LOG_PATH_DEBUG || '/var/log/node/debug.log' ); + return Promise.all( [ + this._conf.get( 'log.priority' ), + this._conf.get( 'log.debug.path' ), + ] ) + .then( ([ priority, debug_log_path ]) => + { + this._debugLogPath = debug_log_path; - return this.getPriorityLog()( - this._debugLogPath, - ( process.env.LOG_PRIORITY || log_priority ) - ); + return this.getPriorityLog()( + debug_log_path, + priority + ) + } ); }, @@ -514,25 +532,33 @@ module.exports = AbstractClass( 'Daemon', this._debugLog ); - this._httpServer.listen( this._httpPort, function() - { - _self._debugLog.log( - 1, "Server running on port %d", _self._httpPort - ); + this._conf.get( 'http.port' ) + .then( port => this._httpServer.listen( port, () => + { + this._debugLog.log( + 1, "Server running on port %d", _self._httpPort + ); - callback(); - } ); + callback(); + } ) ) + .catch( e => this._httpError( e ) ); } - catch( err ) + catch( e ) { - this._debugLog.log( this._debugLog.PRIORITY_ERROR, - "Unable to start HTTP server: %s", - err - ); - - // exit with an error - process.exit( 1 ); + this._httpError( e ); } }, + + + 'private _httpError'( e ) + { + this._debugLog.log( this._debugLog.PRIORITY_ERROR, + "Unable to start HTTP server: %s", + err + ); + + // TODO: use daemon-level promise and reject it + process.exit( 1 ); + }, } ); diff --git a/src/server/daemon/scripts.js b/src/server/daemon/scripts.js index a04127f..0f47b5f 100644 --- a/src/server/daemon/scripts.js +++ b/src/server/daemon/scripts.js @@ -51,10 +51,12 @@ var script_paths = [ ( process.env.LV_ROOT_PATH || '.' ) + '/src/www/scripts/program/', ]; +const legacy_path = process.env.LV_LEGACY_PATH + '/'; + var script_prefix = { liza: __dirname + '/../../', assert: __dirname + '/../../assert/', - program: ( process.env.LV_LEGACY_PATH + '/' ) || '', + program: ( legacy_path + 'program/' ) || '', }; /** @@ -103,7 +105,7 @@ exports.route = function( request, log ) suffix = parts[ 2 ]; var chk_paths = script_paths.slice(); - chk_paths.unshift( script_prefix[ prefix ] || './' ); + chk_paths.unshift( script_prefix[ prefix ] || legacy_path ); // check each of the paths for the script that was requested ( function check_path( paths ) @@ -119,6 +121,7 @@ exports.route = function( request, log ) // check to see if the file exists within the path var filename = ( cur_path + suffix ); + fs.exists( filename, function( exists ) { if ( !exists )