Refactor out server init to index.js

This commit is contained in:
calzoneman 2013-10-11 15:48:01 -05:00
parent 128367bfc4
commit 542461a533
4 changed files with 51 additions and 30 deletions

27
index.js Normal file
View file

@ -0,0 +1,27 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Server = require("./lib/server");
var Config = require("./lib/config");
Config.load("cfg.json", function (cfg) {
var sv = Server.init(cfg);
if(!cfg["debug"]) {
process.on("uncaughtException", function (err) {
Logger.errlog.log("[SEVERE] Uncaught Exception: " + err);
Logger.errlog.log(err.stack);
});
process.on("SIGINT", function () {
sv.shutdown();
});
}
});

View file

@ -62,7 +62,7 @@ function save(cfg, file) {
fs.writeFileSync(file, JSON.stringify(x, null, 4));
}
exports.load = function (Server, file, callback) {
exports.load = function (file, callback) {
var cfg = {};
for(var k in defaults)
cfg[k] = defaults[k];
@ -102,7 +102,6 @@ exports.load = function (Server, file, callback) {
cfg["loaded"] = true;
save(cfg, file);
Server.cfg = cfg;
callback();
callback(cfg);
});
}

View file

@ -1,3 +1,14 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var path = require("path");
var fs = require("fs");
var http = require("http");
@ -276,15 +287,6 @@ var singleton = null;
module.exports = {
init: function (cfg) {
singleton = new Server(cfg);
return singleton;
},
getServer: function () {
return singleton;
}
};
Logger.syslog.log("Starting CyTube v" + VERSION);
var chanlogpath = path.join(__dirname, "../chanlogs");
fs.exists(chanlogpath, function (exists) {
@ -295,18 +297,11 @@ var chandumppath = path.join(__dirname, "../chandump");
fs.exists(chandumppath, function (exists) {
exists || fs.mkdir(chandumppath);
});
singleton = new Server(cfg);
return singleton;
},
var x = {};
Config.load(x, path.join(__dirname, "../cfg.json"), function () {
module.exports.init(x.cfg);
if(!singleton.cfg["debug"]) {
process.on("uncaughtException", function (err) {
Logger.errlog.log("[SEVERE] Uncaught Exception: " + err);
Logger.errlog.log(err.stack);
});
process.on("SIGINT", function () {
singleton.shutdown();
});
getServer: function () {
return singleton;
}
});
};

2
run.sh
View file

@ -2,7 +2,7 @@
while :
do
node lib/server.js
node index.js
sleep 2
done