mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
146 lines
3.4 KiB
JavaScript
146 lines
3.4 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
/*
|
|
|
|
Server
|
|
============
|
|
|
|
>This is the main module that is used to run the whole server:
|
|
>
|
|
> node server [log_type http_port]
|
|
>
|
|
>Valid `log_type`'s are:
|
|
>
|
|
>- `0`: standard I/O output (default)
|
|
>- `1`: log file (server.log)
|
|
>- `2`: silent
|
|
>
|
|
>`http_port` can be set to use another port, than defined in the
|
|
>[config](config.html) file, to listen to, e.g. used by the test suite.
|
|
>
|
|
>
|
|
|
|
TODO how about we allow spawning child processes with servers based on address?
|
|
*/
|
|
|
|
|
|
(function() {
|
|
var args, conf, db, engine, http_listener, init, log, procCmds, shutDown;
|
|
|
|
log = require('./logging');
|
|
|
|
conf = require('./config');
|
|
|
|
db = require('./persistence');
|
|
|
|
engine = require('./engine');
|
|
|
|
http_listener = require('./http_listener');
|
|
|
|
args = {};
|
|
|
|
procCmds = {};
|
|
|
|
/*
|
|
Error handling of the express port listener requires special attention,
|
|
thus we have to catch the process error, which is issued if
|
|
the port is already in use.
|
|
*/
|
|
|
|
|
|
process.on('uncaughtException', function(err) {
|
|
switch (err.errno) {
|
|
case 'EADDRINUSE':
|
|
err.addInfo = 'http_port already in use, shutting down!';
|
|
log.error('RS', err);
|
|
return shutDown();
|
|
default:
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
/*
|
|
This function is invoked right after the module is loaded and starts the server.
|
|
|
|
@private init()
|
|
*/
|
|
|
|
|
|
init = function() {
|
|
log.print('RS', 'STARTING SERVER');
|
|
conf(args);
|
|
if (!conf.isReady()) {
|
|
log.error('RS', 'Config file not ready!');
|
|
process.exit();
|
|
}
|
|
if (process.argv.length > 2) {
|
|
args.logType = parseInt(process.argv[2]) || 0;
|
|
switch (args.logType) {
|
|
case 0:
|
|
log.print('RS', 'Log type set to standard I/O output');
|
|
break;
|
|
case 1:
|
|
log.print('RS', 'Log type set to file output');
|
|
break;
|
|
case 2:
|
|
log.print('RS', 'Log type set to silent');
|
|
break;
|
|
default:
|
|
log.print('RS', 'Unknown log type, using standard I/O');
|
|
}
|
|
log(args);
|
|
} else {
|
|
log.print('RS', 'No log method argument provided, using standard I/O');
|
|
}
|
|
if (process.argv.length > 3) {
|
|
args.http_port = parseInt(process.argv[3]);
|
|
} else {
|
|
log.print('RS', 'No HTTP port passed, using standard port from config file');
|
|
}
|
|
log.print('RS', 'Initialzing DB');
|
|
db(args);
|
|
return db.isConnected(function(err, result) {
|
|
if (!err) {
|
|
log.print('RS', 'Initialzing engine');
|
|
engine(args);
|
|
log.print('RS', 'Initialzing http listener');
|
|
http_listener(args);
|
|
log.print('RS', 'Passing handlers to engine');
|
|
engine.addPersistence(db);
|
|
log.print('RS', 'Passing handlers to http listener');
|
|
return http_listener.addHandlers(shutDown);
|
|
}
|
|
});
|
|
};
|
|
|
|
/*
|
|
Shuts down the server.
|
|
|
|
@private shutDown()
|
|
*/
|
|
|
|
|
|
shutDown = function() {
|
|
log.print('RS', 'Received shut down command!');
|
|
if (engine != null) {
|
|
engine.shutDown();
|
|
}
|
|
return http_listener != null ? http_listener.shutDown() : void 0;
|
|
};
|
|
|
|
/*
|
|
## Process Commands
|
|
|
|
When the server is run as a child process, this function handles messages
|
|
from the parent process (e.g. the testing suite)
|
|
*/
|
|
|
|
|
|
process.on('message', function(cmd) {
|
|
return typeof procCmds[cmd] === "function" ? procCmds[cmd]() : void 0;
|
|
});
|
|
|
|
procCmds.die = shutDown;
|
|
|
|
init();
|
|
|
|
}).call(this);
|