From 8f3aaad7210274d49af81b504cb7fc81158e830a Mon Sep 17 00:00:00 2001 From: Mark Engel Date: Fri, 21 Jan 2011 19:59:09 +0100 Subject: added opts for command line parsing --- src/nodejs/main.js | 44 ++++++++++++++++++++++++-------------------- src/nodejs/package.json | 1 + src/nodejs/xmlGenerator.js | 10 ++++------ 3 files changed, 29 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/nodejs/main.js b/src/nodejs/main.js index 37e106d..17a5235 100644 --- a/src/nodejs/main.js +++ b/src/nodejs/main.js @@ -1,28 +1,29 @@ var clutch = require('clutch'); +var http = require('http'); var pg = require('pg'); var xmlGenerator = require('./xmlGenerator.js'); - -// load config +var opts = require('opts'); var config = require('./config.json'); -process.argv.forEach(function (val,index, array){ - if(val=="-c"){ - path = array[index+1]; +var log4js = require('log4js')(); +var log = log4js.getLogger('global'); + +var options = [ + { short : 'c' + , long : 'config' + , description : 'Select configuration file' + , value : true + , callback : function(value) { loadCustomConfig(value); } + } +]; + +function loadCustomConfig(path) { + log.info("reading custom config: " + path); if( path[0] != '/'){ path = __dirname + '/' + path; } + log.info(path); config = require(path); - } -}); -var connectionString = config['connectionString']; - -//set up logger -var log4js = require('log4js')(); //note the need to call the function -//log4js.addAppender(log4js.fileAppender('osm-xapi.log'), 'cheese'); - -var log = log4js.getLogger('global'); -log.setLevel('ALL'); - -log.info("server starting..."); +} function createWayBboxQuery(key, value, left, bottom, right, top) { return { @@ -117,9 +118,11 @@ function connectionError(err, res) { } function db_connect(res, callback) { - pg.connect(connectionString, function(err, client) { + pg.connect(config['connectionString'], function(err, client) { if(err) { log.error('message'); + console.log(config['connectionString']); + console.log(err); res.writeHead(404,{}); res.end(); } else { @@ -207,7 +210,8 @@ myRoutes = clutch.route404([ //['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler], ]); - -var http = require('http'); +log.setLevel('ALL'); +log.info("server starting..."); +opts.parse(options, true); http.createServer(myRoutes).listen(config.port, config.host); log.info("Started server at " + config.host + ":" + config.port ); diff --git a/src/nodejs/package.json b/src/nodejs/package.json index ca196f4..97db4eb 100644 --- a/src/nodejs/package.json +++ b/src/nodejs/package.json @@ -8,5 +8,6 @@ , "clutch" : "*" , "xmlbuilder" : "*" , "log4js" : "*" + , "opts" : "*" } } diff --git a/src/nodejs/xmlGenerator.js b/src/nodejs/xmlGenerator.js index e982e18..5dc0efa 100644 --- a/src/nodejs/xmlGenerator.js +++ b/src/nodejs/xmlGenerator.js @@ -24,7 +24,7 @@ exports.createNode = function (row) { .att('v',escape(temp[x+1])); } return builder.toString({ pretty: true }); -} +}; exports.createWay = function (row) { var way = builder.begin('way') @@ -39,14 +39,12 @@ exports.createWay = function (row) { .att('k',escape(temp[x])) .att('v',escape(temp[x+1])); } - var temp = row.nodes.replace("{","").replace("}","").split(","); - for(var x=0;x