From 2d0c3b867a90771a8c75eafce2840a5dcb73f55c Mon Sep 17 00:00:00 2001 From: calcnerd256 Date: Sun, 22 Jan 2012 18:42:29 -0600 Subject: [PATCH 1/3] added a hook in server setup for extra initialization --- node/server.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/node/server.js b/node/server.js index 6a4f0cfe2..450c5188a 100644 --- a/node/server.js +++ b/node/server.js @@ -66,6 +66,8 @@ exports.maxAge = 1000*60*60*6; //set loglevel log4js.setGlobalLogLevel(settings.loglevel); +function init(additionalSetup){ + if("function" != typeof additionalSetup) additionalSetup = function(){}; async.waterfall([ //initalize the database function (callback) @@ -423,6 +425,20 @@ async.waterfall([ } }); }); + + additionalSetup( + app, db, + { + ro: readOnlyManager, + pad: padManager, + security: securityManager + }, + { + "export": exportHandler, + "import": importHandler, + api: apiHandler + } + ); //let the server listen app.listen(settings.port, settings.ip); @@ -514,3 +530,5 @@ async.waterfall([ callback(null); } ]); +} +init(function(){}); From 43af5cfe00b52c06dcddf41ce022a435427c0cf4 Mon Sep 17 00:00:00 2001 From: calcnerd256 Date: Sun, 22 Jan 2012 18:51:42 -0600 Subject: [PATCH 2/3] separated the server module from the serve script which instantiates an instance of it --- bin/debugRun.sh | 2 +- bin/run.sh | 2 +- node/serve.js | 1 + node/server.js | 2 +- start.bat | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 node/serve.js diff --git a/bin/debugRun.sh b/bin/debugRun.sh index 01197a6b8..b963744d8 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -23,7 +23,7 @@ node-inspector & echo "If you are new to node-inspector, take a look at this video: http://youtu.be/AOnK3NVnxL8" cd "node" -node --debug server.js +node --debug serve.js #kill node-inspector before ending kill $! diff --git a/bin/run.sh b/bin/run.sh index a5245ff77..878e0a55c 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -26,4 +26,4 @@ bin/installDeps.sh || exit 1 #Move to the node folder and start echo "start..." cd "node" -node server.js +node serve.js diff --git a/node/serve.js b/node/serve.js new file mode 100644 index 000000000..e0e75c317 --- /dev/null +++ b/node/serve.js @@ -0,0 +1 @@ +require("./server").init(function(){}); diff --git a/node/server.js b/node/server.js index 450c5188a..cb368db32 100644 --- a/node/server.js +++ b/node/server.js @@ -531,4 +531,4 @@ async.waterfall([ } ]); } -init(function(){}); +this.init = init; diff --git a/start.bat b/start.bat index cf6a60f18..0fe44b3f9 100644 --- a/start.bat +++ b/start.bat @@ -1,2 +1,2 @@ cd node -..\bin\node server.js +..\bin\node serve.js From 23d63697460d6636285aa79c895c49c2427c3a5d Mon Sep 17 00:00:00 2001 From: calcnerd256 Date: Mon, 30 Jan 2012 10:47:06 -0600 Subject: [PATCH 3/3] added some comments to the server setup to explain what additionalSetup does and why --- node/server.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/node/server.js b/node/server.js index 42f86daff..4f3ef9bad 100644 --- a/node/server.js +++ b/node/server.js @@ -68,8 +68,14 @@ exports.maxAge = 1000*60*60*6; log4js.setGlobalLogLevel(settings.loglevel); function init(additionalSetup){ - if("function" != typeof additionalSetup) additionalSetup = function(){}; -async.waterfall([ + //additionalSetup is an optional argument that must be a function + //the purpose of additionalSetup is + //to allow whoever initializes the server to add functionality + //before the server begins listening + if("function" != typeof additionalSetup) + //the default value for initialSetup is a no-op + additionalSetup = function additionalSetup(app, db, managers, handlers){}; + async.waterfall([ //initalize the database function (callback) { @@ -414,10 +420,13 @@ async.waterfall([ }); }); + //additionalSetup is a hook for modifying the app before it begins listening + //some things such a hook might need are the app itself, the database, and any managers and handlers it might want to use + //rather than passing the managers and handlers one by one, each of those collections is passed as a dictionary additionalSetup( app, db, { - ro: readOnlyManager, + "ro": readOnlyManager, pad: padManager, security: securityManager },