Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bin/server.js66
-rw-r--r--conf/vanilla-server.json55
-rw-r--r--src/server/daemon/Daemon.js122
-rw-r--r--src/server/daemon/scripts.js7
4 files changed, 200 insertions, 50 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+'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 )