2017-08-28 12:37:46 -04:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
|
2017-09-08 14:18:16 -04:00
|
|
|
const fs = require( 'fs' );
|
|
|
|
const path = require( 'path' );
|
2017-08-28 12:37:46 -04:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
2017-09-08 14:18:16 -04:00
|
|
|
const conf_dir = path.dirname( conf_path );
|
2017-08-28 12:37:46 -04:00
|
|
|
|
|
|
|
ConfLoader( fs, ConfStore )
|
|
|
|
.fromFile( conf_path )
|
|
|
|
.then( conf => Promise.all( [
|
|
|
|
conf.get( 'name' ),
|
|
|
|
conf.get( 'daemon' ),
|
2017-09-12 15:29:43 -04:00
|
|
|
conf.get( 'pidfile' ),
|
2017-08-28 12:37:46 -04:00
|
|
|
Promise.resolve( conf ),
|
|
|
|
] ) )
|
2017-09-12 15:29:43 -04:00
|
|
|
.then( ([ name, daemon, pidfile, conf ]) =>
|
2017-08-28 12:37:46 -04:00
|
|
|
{
|
2017-09-14 15:44:09 -04:00
|
|
|
const daemon_path = _resolvePath( conf_dir, daemon );
|
|
|
|
const pid_path = _resolvePath( conf_dir, ( pidfile || ".pid" ) );
|
2017-09-12 15:29:43 -04:00
|
|
|
|
|
|
|
writePidFile( pid_path );
|
|
|
|
greet( name, pid_path );
|
2017-09-08 14:18:16 -04:00
|
|
|
|
|
|
|
return require( daemon_path )( conf ).start();
|
2017-08-28 12:37:46 -04:00
|
|
|
} )
|
|
|
|
.catch( e => {
|
|
|
|
console.error( e.stack );
|
|
|
|
process.exit( 1 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-09-14 15:44:09 -04:00
|
|
|
/**
|
|
|
|
* Produce an absolute path if `path` is absolute, otherwise a path relative
|
|
|
|
* to the configuration directory
|
|
|
|
*
|
|
|
|
* @param {string} conf_path configuration path (for relative `path`)
|
|
|
|
* @param {string} path path to resolve
|
|
|
|
*
|
|
|
|
* @return resolved path
|
|
|
|
*/
|
|
|
|
function _resolvePath( conf_path, path )
|
|
|
|
{
|
|
|
|
return ( path[ 0 ] === '/' )
|
|
|
|
? path
|
|
|
|
: conf_path + '/' + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-12 15:29:43 -04:00
|
|
|
function writePidFile( pid_path )
|
|
|
|
{
|
|
|
|
fs.writeFile( pid_path, process.pid );
|
|
|
|
|
|
|
|
process.on( 'exit', () => fs.unlink( pid_path ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function greet( name, pid_path )
|
2017-08-28 12:37:46 -04:00
|
|
|
{
|
|
|
|
console.log( `${name} (liza-${version})`);
|
|
|
|
console.log( `Server configuration: ${conf_path}` );
|
2017-09-12 15:29:43 -04:00
|
|
|
console.log( `PID file: ${pid_path}` );
|
2017-08-28 12:37:46 -04:00
|
|
|
}
|