Add a settings file

This commit is contained in:
Peter 'Pita' Martischka 2011-05-14 17:22:25 +01:00
parent 7c4502951a
commit 8da3c04b41
3 changed files with 48 additions and 1 deletions

View File

@ -19,6 +19,7 @@ var http = require('http')
, fs = require('fs') , fs = require('fs')
, io = require('socket.io') , io = require('socket.io')
, sys = require('sys') , sys = require('sys')
, settings = require('./settings')
, server; , server;
server = http.createServer(function(req, res){ server = http.createServer(function(req, res){
@ -56,7 +57,7 @@ server = http.createServer(function(req, res){
send404(res, path); send404(res, path);
} }
}); });
server.listen(9001); server.listen(settings.port);
console.log("Server is listening at port 9001"); console.log("Server is listening at port 9001");
function randomPadName() { function randomPadName() {

43
node/settings.js Normal file
View File

@ -0,0 +1,43 @@
var fs = require("fs");
//default settings
exports.port = 9001;
//read the settings sync
var settingsStr = fs.readFileSync("../settings.json");
//try to parse the settings
var settings;
try
{
settings = JSON.parse(settingsStr);
}
catch(e)
{
console.error("There is a syntax error in your settings.json file");
console.error("We recommend http://jsonlint.com/ to find the mistake");
console.error(e.stack);
process.exit(1);
}
//loop trough the settings
for(var i in settings)
{
//test if the setting start with a low character
if(i.charAt(0).search("[a-z]") !== 0)
{
console.error("WARNING: Settings should start with a low character: '" + i + "'");
}
//we know this setting, so we overwrite it
if(exports[i])
{
exports[i] = settings[i];
}
//this setting is unkown, output a warning and throw it away
else
{
console.error("WARNING: Unkown Setting: '" + i + "'");
console.error("If this isn't a mistake, add the default settings for this value to node/settings.js");
}
}

3
settings.json Normal file
View File

@ -0,0 +1,3 @@
{
"port":9001
}