refactoring and techdoc started

This commit is contained in:
Dominic Bosch 2014-02-18 09:48:18 +01:00
parent 264ef6eeb6
commit d32eae6565
17 changed files with 79 additions and 88 deletions

1
.gitignore vendored
View file

@ -2,6 +2,5 @@
*.log
*credentials.json
*node_modules
doc
.project
.settings

View file

@ -2,9 +2,9 @@
HTTP Listener
=============
> Receives the HTTP requests to the server at the port specified by the
> [config](config.html) file. These requests (bound to a method) are then
> redirected to the appropriate handler which then takes care of the request.
> Receives the HTTP requests to the server at the given port. The requests
> (bound to a method) are then redirected to the appropriate handler which
> takes care of the request.
###
@ -13,12 +13,8 @@ HTTP Listener
# - [Logging](logging.html)
log = require './logging'
# - [Config](config.html)
config = require './config'
# TODO remove config
# - [User Handler](user_handler.html)
requestHandler = require './request_handler'
# - [Request Handler](request-handler.html)
requestHandler = require './request-handler'
# - Node.js Modules: [path](http://nodejs.org/api/path.html) and
# [querystring](http://nodejs.org/api/querystring.html)
@ -34,33 +30,29 @@ app = express()
###
Module call
-----------
Initializes the HTTP Listener and its child modules Logging,
Configuration and Request Handler, then tries to fetch the session
key from the configuration.
Initializes the HTTP listener and its request handler.
@param {Object} args
###
exports = module.exports = ( args ) ->
args = args ? {}
log args
config args
requestHandler args
initRouting args[ 'http-port' ]
module.exports
###
Adds the shutdown handler to the admin commands.
Initializes the request routing and starts listening on the given port.
@param {function} fshutDown
@public addHandlers( *fShutDown* )
@param {int} port
@private initRouting( *fShutDown* )
###
exports.addHandlers = ( fShutDown ) ->
requestHandler.addShutdownHandler fShutDown
initRouting = ( port ) ->
# Add cookie support for session handling.
app.use express.cookieParser()
#TODO This needs to be fixed!
#TODO The session secret appriach needs to be fixed!
sess_sec = "149u*y8C:@kmN/520Gt\\v'+KFBnQ!\\r<>5X/xRI`sT<Iw"
app.use express.session { secret: sess_sec }
# app.use express.session { secret: config.getSessionSecret() }
#At the moment there's no redis session backbone (didn't work straight away)
log.print 'HL', 'no session backbone'
@ -91,15 +83,20 @@ exports.addHandlers = ( fShutDown ) ->
# - **`POST` to _"/user"_:** User requests are possible for all users with an account
app.post '/usercommand', requestHandler.handleUserCommand
try
http_port = config.getHttpPort()
if http_port
app.listen http_port # inbound event channel
else
log.error 'HL', new Error 'No HTTP port found!? Nothing to listen on!...'
app.listen port # inbound event channel
catch e
e.addInfo = 'opening port'
log.error e
###
Adds the shutdown handler to the admin commands.
@param {function} fshutDown
@public addShutdownHandler( *fShutDown* )
###
exports.addShutdownHandler = ( fShutDown ) ->
requestHandler.addShutdownHandler fShutDown
###
Shuts down the http listener.

View file

@ -14,8 +14,8 @@ log = require './logging'
# - [Persistence](persistence.html)
db = require './persistence'
# - [Module Manager](module_manager.html)
mm = require './module_manager'
# - [Module Manager](module-manager.html)
mm = require './module-manager'
# - Node.js Modules: [fs](http://nodejs.org/api/fs.html),
# [path](http://nodejs.org/api/path.html) and
@ -37,6 +37,7 @@ objUserCmds =
'get_eventmodules': mm.getAllEventModules
'store_rule': mm.storeRule
objAdminCmds = {}
exports = module.exports = ( args ) ->
args = args ? {}
@ -56,7 +57,7 @@ The shutdown function will be called if the admin command shutdown is issued.
@param {function} fShutdown
###
exports.addShutdownHandler = ( fShutdown ) =>
@objAdminCmds.shutdown = ( args, answerHandler ) ->
objAdminCmds.shutdown = ( args, answerHandler ) ->
answerHandler.answerSuccess 'Shutting down... BYE!'
setTimeout fShutdown, 500

View file

@ -13,7 +13,7 @@ Server
# **Requires:**
# - [Logging](logging.html)
logger = require './new_logging'
logger = require './new-logging'
# - [Configuration](config.html)
conf = require './config'
@ -24,8 +24,8 @@ db = require './persistence'
# - [Engine](engine.html)
engine = require './engine'
# - [HTTP Listener](http_listener.html)
http_listener = require './http_listener'
# - [HTTP Listener](http-listener.html)
http = require './http-listener'
# - Node.js Modules: [fs](http://nodejs.org/api/fs.html),
# [path](http://nodejs.org/api/path.html)
@ -133,14 +133,14 @@ init = ->
log.info 'RS | Initialzing engine'
engine args
log.info 'RS | Initialzing http listener'
http_listener args
http args
# > Distribute handlers between modules to link the application.
log.info 'RS | Passing handlers to engine'
engine.addPersistence db
log.info 'RS | Passing handlers to http listener'
#TODO engine pushEvent needs to go into redis queue
http_listener.addShutdownHandler shutDown
http.addShutdownHandler shutDown
#TODO loadAction and addRule will be removed
#mm.addHandlers db, engine.loadActionModule, engine.addRule
log.info 'RS | For e child process for the event poller'
@ -151,7 +151,7 @@ init = ->
args.logconf['file-path']
args.logconf['nolog']
]
poller = cp.fork path.resolve( __dirname, 'event_poller' ), cliArgs
poller = cp.fork path.resolve( __dirname, 'event-poller' ), cliArgs
###
Shuts down the server.
@ -161,7 +161,7 @@ Shuts down the server.
shutDown = ->
log.warn 'RS | Received shut down command!'
engine?.shutDown()
http_listener?.shutDown()
http?.shutDown()
###
## Process Commands

View file

@ -1,10 +0,0 @@
load rules rules
load rules
load action probinder
load actions
load event probinder
load events
-> all these from server started from shell and node (different cwd!)
shutdown
upload rules/actions/events
look at doc ([host]/doc/)

View file

@ -7,12 +7,12 @@ var path = require('path'),
listRules = {},
listActionModules = {},
isRunning = true,
ml, poller, db;
mm, poller, db;
exports = module.exports = function( args ) {
args = args || {};
log(args);
ml = require('./module_loader')(args);
mm = require('./module-manager')(args);
return module.exports;
};
@ -24,7 +24,7 @@ exports = module.exports = function( args ) {
*/
exports.addPersistence = function(db_link) {
db = db_link;
// if(ml && db) db.getActionModules(function(err, obj) {
// if(mm && db) db.getActionModules(function(err, obj) {
// if(err) log.error('EN', 'retrieving Action Modules from DB!');
// else {
// if(!obj) {
@ -35,7 +35,7 @@ exports.addPersistence = function(db_link) {
// for(var el in obj) {
// log.print('EN', 'Loading Action Module from DB: ' + el);
// try{
// m = ml.requireFromString(obj[el], el);
// m = mm.requireFromString(obj[el], el);
// db.getActionModuleAuth(el, function(mod) {
// return function(err, obj) {
// if(obj && mod.loadCredentials) mod.loadCredentials(JSON.parse(obj));

View file

@ -2,7 +2,7 @@
'use strict';
var logger = require('./new_logging'),
var logger = require('./new-logging'),
listMessageActions = {},
listAdminCommands = {},
listEventModules = {},
@ -31,7 +31,7 @@ function init() {
log = logger(logconf);
var args = { logger: log };
(ml = require('./module_manager'))(args);
(ml = require('./module-manager'))(args);
(db = require('./persistence'))(args);
initAdminCommands();
initMessageActions();

View file

@ -3,20 +3,18 @@
HTTP Listener
=============
> Receives the HTTP requests to the server at the port specified by the
> [config](config.html) file. These requests (bound to a method) are then
> redirected to the appropriate handler which then takes care of the request.
> Receives the HTTP requests to the server at the given port. The requests
> (bound to a method) are then redirected to the appropriate handler which
> takes care of the request.
*/
(function() {
var app, config, exports, express, log, path, qs, requestHandler;
var app, exports, express, initRouting, log, path, qs, requestHandler;
log = require('./logging');
config = require('./config');
requestHandler = require('./request_handler');
requestHandler = require('./request-handler');
path = require('path');
@ -29,9 +27,7 @@ HTTP Listener
/*
Module call
-----------
Initializes the HTTP Listener and its child modules Logging,
Configuration and Request Handler, then tries to fetch the session
key from the configuration.
Initializes the HTTP listener and its request handler.
@param {Object} args
*/
@ -40,22 +36,21 @@ HTTP Listener
exports = module.exports = function(args) {
args = args != null ? args : {};
log(args);
config(args);
requestHandler(args);
initRouting(args['http-port']);
return module.exports;
};
/*
Adds the shutdown handler to the admin commands.
Initializes the request routing and starts listening on the given port.
@param {function} fshutDown
@public addHandlers( *fShutDown* )
@param {int} port
@private initRouting( *fShutDown* )
*/
exports.addHandlers = function(fShutDown) {
var e, http_port, sess_sec;
requestHandler.addShutdownHandler(fShutDown);
initRouting = function(port) {
var e, sess_sec;
app.use(express.cookieParser());
sess_sec = "149u*y8C:@kmN/520Gt\\v'+KFBnQ!\\r<>5X/xRI`sT<Iw";
app.use(express.session({
@ -72,12 +67,7 @@ HTTP Listener
app.post('/logout', requestHandler.handleLogout);
app.post('/usercommand', requestHandler.handleUserCommand);
try {
http_port = config.getHttpPort();
if (http_port) {
return app.listen(http_port);
} else {
return log.error('HL', new Error('No HTTP port found!? Nothing to listen on!...'));
}
return app.listen(port);
} catch (_error) {
e = _error;
e.addInfo = 'opening port';
@ -85,6 +75,18 @@ HTTP Listener
}
};
/*
Adds the shutdown handler to the admin commands.
@param {function} fshutDown
@public addShutdownHandler( *fShutDown* )
*/
exports.addShutdownHandler = function(fShutDown) {
return requestHandler.addShutdownHandler(fShutDown);
};
/*
Shuts down the http listener.

View file

@ -1 +0,0 @@
// Store debug information on first compilation and return it to the user

View file

@ -8,14 +8,14 @@ Request Handler
(function() {
var answerHandler, crypto, db, exports, fs, getHandlerFileAsString, getHandlerPath, getIncludeFileAsString, log, mm, mustache, objUserCmds, path, qs, renderPage, sendLoginOrPage,
var answerHandler, crypto, db, exports, fs, getHandlerFileAsString, getHandlerPath, getIncludeFileAsString, log, mm, mustache, objAdminCmds, objUserCmds, path, qs, renderPage, sendLoginOrPage,
_this = this;
log = require('./logging');
db = require('./persistence');
mm = require('./module_manager');
mm = require('./module-manager');
fs = require('fs');
@ -35,6 +35,8 @@ Request Handler
'store_rule': mm.storeRule
};
objAdminCmds = {};
exports = module.exports = function(args) {
var user, users, _i, _len;
args = args != null ? args : {};
@ -60,7 +62,7 @@ Request Handler
exports.addShutdownHandler = function(fShutdown) {
return _this.objAdminCmds.shutdown = function(args, answerHandler) {
return objAdminCmds.shutdown = function(args, answerHandler) {
answerHandler.answerSuccess('Shutting down... BYE!');
return setTimeout(fShutdown, 500);
};

View file

@ -13,9 +13,9 @@ Server
(function() {
var argv, conf, cp, db, engine, fs, http_listener, init, logger, opt, optimist, path, procCmds, shutDown, usage;
var argv, conf, cp, db, engine, fs, http, init, logger, opt, optimist, path, procCmds, shutDown, usage;
logger = require('./new_logging');
logger = require('./new-logging');
conf = require('./config');
@ -23,7 +23,7 @@ Server
engine = require('./engine');
http_listener = require('./http_listener');
http = require('./http-listener');
fs = require('fs');
@ -154,14 +154,14 @@ Server
log.info('RS | Initialzing engine');
engine(args);
log.info('RS | Initialzing http listener');
http_listener(args);
http(args);
log.info('RS | Passing handlers to engine');
engine.addPersistence(db);
log.info('RS | Passing handlers to http listener');
http_listener.addShutdownHandler(shutDown);
http.addShutdownHandler(shutDown);
log.info('RS | For e child process for the event poller');
cliArgs = [args.logconf['mode'], args.logconf['io-level'], args.logconf['file-level'], args.logconf['file-path'], args.logconf['nolog']];
return poller = cp.fork(path.resolve(__dirname, 'event_poller'), cliArgs);
return poller = cp.fork(path.resolve(__dirname, 'event-poller'), cliArgs);
}
});
};
@ -178,7 +178,7 @@ Server
if (engine != null) {
engine.shutDown();
}
return http_listener != null ? http_listener.shutDown() : void 0;
return http != null ? http.shutDown() : void 0;
};
/*

View file

@ -1,3 +1,3 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
node $DIR/js-coffee/server | $DIR/node_modules/bunyan/bin/bunyan
node $DIR/js-coffee/webapi-eca | $DIR/node_modules/bunyan/bin/bunyan

View file

@ -6,7 +6,7 @@ exports.setUp = ( cb ) =>
try
@fs.unlinkSync @stdPath
catch e
@log = require @path.join '..', 'js-coffee', 'new_logging'
@log = require @path.join '..', 'js-coffee', 'new-logging'
cb()
exports.tearDown = ( cb ) =>

1
webpages/public/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
doc