From 2d370398fe8b2329736d839504999186a1aee0aa Mon Sep 17 00:00:00 2001 From: slomo Date: Wed, 12 Jan 2011 22:58:31 +0100 Subject: some new tries --- src/nodejs/no1.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/nodejs/no1.js (limited to 'src/nodejs/no1.js') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js new file mode 100644 index 0000000..feb3dfb --- /dev/null +++ b/src/nodejs/no1.js @@ -0,0 +1,17 @@ +var clutch = require('clutch'); + +function helloSomeone(req, res, name,bbox, key, value) { + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end('obj:'+name+ ' bbox: '+ bbox + ' key:' +key +' value:'+value+'!\n'); +} + +function helloWorld(req, res) { + helloSomeone(req, res, 'World'); +} + +myRoutes = clutch.route404([['GET /hello/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], + ['GET /hello/$', helloWorld]]); + + +var http = require('http'); +http.createServer(myRoutes).listen(3000, '127.0.0.1'); -- cgit v1.2.3 From 7d9e6e0d1bd45bedc030a287330c50cde7769f56 Mon Sep 17 00:00:00 2001 From: Philipp Borgers Date: Thu, 13 Jan 2011 01:49:48 +0100 Subject: fuck you regex... database request possible. we have to learn regex!!! --- src/nodejs/no1.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 7 deletions(-) (limited to 'src/nodejs/no1.js') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index feb3dfb..1a250be 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -1,17 +1,85 @@ + var clutch = require('clutch'); +var pg = require('pg') + +var connectionString = "pg://user:password@host/database"; + + +function createNodeBboxQuery(key, value, left, bottom, right, top) { + return "SELECT * from nodes WHERE (tags @> '\"" + key + + "\"=>\"" + value + "\"'" + + " AND POINT(geom) @ polygon(box('(" + left + + "," + bottom +")'::point,'(" + + + right + "," + top + ")'::point)));"; +} + + + +function nodeWorldHandler(req, res, key, value) { + + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end(' key:' +key +' value:'+value+'\n'); +} +function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { + pg.connect(connectionString, function(err,client) { + + if (err) { + console.log(err); + } + else { + console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); + /*client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { + + if (err) { + console.log(err); + } + else { + console.log(result.rows); + for(row in result.rows.length) { + console.log(row); + } + } + });*/ + } + + + + }); + + + + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end( 'bbox: '+ left + bottom + right + top + ' key:' +key +' value:'+value+'!\n'); +} + +function wayWorldHandler(req, res, key, value) { + + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end(' key:' +key +' value:'+value+'!\n'); +} +function wayBboxHandler(req, res, key, value, bbox, left, bottom, right, top) { +} -function helloSomeone(req, res, name,bbox, key, value) { +function relationWorldHandler(req, res, key, value) { + res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end('obj:'+name+ ' bbox: '+ bbox + ' key:' +key +' value:'+value+'!\n'); + res.end(' key:' +key +' value:'+value+'!\n'); } +function relationBboxHandler(req, res, key, value, left, bottom, right, top) { -function helloWorld(req, res) { - helloSomeone(req, res, 'World'); } -myRoutes = clutch.route404([['GET /hello/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], - ['GET /hello/$', helloWorld]]); +myRoutes = clutch.route404([ + //['GET /api/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], + ['GET /api/node\\[(\\w+)=(\\w+)\\]$',nodeWorldHandler], + ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(\\.\\d+)?),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+\\.\\d+),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + ['GET /api/way\\[(\\w+)=(\\w+)\\]$',wayWorldHandler], + ['GET /api/way\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d),(\\d),(\\d),(\\d)\\]$',wayBboxHandler], + ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler], + ['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler], + ]); var http = require('http'); -http.createServer(myRoutes).listen(3000, '127.0.0.1'); +http.createServer(myRoutes).listen(8080, 'localhost'); -- cgit v1.2.3 From 73990eee3cd27767800b5713b378d552cdbe6276 Mon Sep 17 00:00:00 2001 From: Philipp Borgers Date: Thu, 13 Jan 2011 19:32:17 +0100 Subject: fight against regex succesful, fixed database request, added xmlbuilder, return xml --- src/nodejs/no1.js | 61 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'src/nodejs/no1.js') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 1a250be..8eb86d7 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -1,16 +1,22 @@ var clutch = require('clutch'); var pg = require('pg') +var builder = require('xmlbuilder') var connectionString = "pg://user:password@host/database"; function createNodeBboxQuery(key, value, left, bottom, right, top) { - return "SELECT * from nodes WHERE (tags @> '\"" + key + /* + * return "SELECT * from nodes WHERE (tags @> '\"" + key + "\"=>\"" + value + "\"'" + " AND POINT(geom) @ polygon(box('(" + left + "," + bottom +")'::point,'(" + + right + "," + top + ")'::point)));"; + */ + + return "SELECT id,tstamp,version,changeset_id, X(geom) as lat, Y(geom) as lon FROM nodes WHERE (tags @> hstore('" + key + "','" + value + "') AND geom && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; } @@ -25,37 +31,67 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { if (err) { console.log(err); + res.writeHead(404,{}); + res.end('\n'); } else { console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); - /*client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { + client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { if (err) { console.log(err); + res.writeHead(404,{}); + res.end('\n'); } else { console.log(result.rows); - for(row in result.rows.length) { - console.log(row); + + res.writeHead(200, {'Content-Type': 'text/plain'}); + //res.write("lala"); + res.write(""); + for(var i=0; i"); + res.write(""); + //console.log(result.rows[i].id); + */ + var node = builder.begin('node') + .att('id', result.rows[i].id) + .att('timetamp', result.rows[i].tstamp) + .att('version', result.rows[i].version) + .att('changeset', result.rows[i].changeset_id) + .att('lat', result.rows[i].lat) + .att('lon', result.rows[i].lon); + + + + res.write(builder.toString()); + } + res.write(""); + res.end(); } - });*/ + }); } }); + + //console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); - - - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end( 'bbox: '+ left + bottom + right + top + ' key:' +key +' value:'+value+'!\n'); + //res.writeHead(200, {'Content-Type': 'text/plain'}); + //res.end( 'bbox: '+ left + bottom + right + top + ' key:' +key +' value:'+value+'!\n'); } function wayWorldHandler(req, res, key, value) { res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end(' key:' +key +' value:'+value+'!\n'); + } function wayBboxHandler(req, res, key, value, bbox, left, bottom, right, top) { } @@ -72,8 +108,9 @@ function relationBboxHandler(req, res, key, value, left, bottom, right, top) { myRoutes = clutch.route404([ //['GET /api/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], ['GET /api/node\\[(\\w+)=(\\w+)\\]$',nodeWorldHandler], - ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(\\.\\d+)?),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], - ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+\\.\\d+),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(\\.\\d+)?),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',nodeBboxHandler], + //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+\\.\\d+),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], ['GET /api/way\\[(\\w+)=(\\w+)\\]$',wayWorldHandler], ['GET /api/way\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d),(\\d),(\\d),(\\d)\\]$',wayBboxHandler], ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler], -- cgit v1.2.3 From 509d0a07c6131c50454f91ef1f0d0085b551b95f Mon Sep 17 00:00:00 2001 From: slomo Date: Fri, 21 Jan 2011 15:57:13 +0100 Subject: added log4js --- src/nodejs/no1.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/nodejs/no1.js') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 7fcdf93..f55f736 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -2,26 +2,34 @@ var clutch = require('clutch'); var pg = require('pg'); var builder = require('xmlbuilder'); -var config = require('./config.json'); + + // load config +var config = require('./config.json'); process.argv.forEach( function (val,index, array){ if(val=="-c"){ path = array[index+1]; - console.log(path[0]); if( path[0] != '/'){ path = __dirname + '/' + path; } config = require(path); } }); - var connectionString = config['connectionString']; -console.log("server starting..."); -console.log("Connection String: " + connectionString); + + +//set up logger +var log4js = require('log4js')(); //note the need to call the function +//log4js.addAppender(log4js.fileAppender('osm-xapi.log'), 'cheese'); + +var logger = log4js.getLogger('global'); +logger.setLevel('ALL'); + +logger.info("server starting..."); function toISO8601(date) { //2007-03-31T00:09:22+01:00 @@ -268,4 +276,4 @@ myRoutes = clutch.route404([ var http = require('http'); http.createServer(myRoutes).listen(config.port, config.host); -console.log("Started server at " + config.host + ":" + config.port ); +logger.info("Started server at " + config.host + ":" + config.port ); -- cgit v1.2.3 From f7857666f01591e50c9ef377651710da3455eef4 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 21 Jan 2011 15:58:36 +0100 Subject: removed trailing whitespaces --- src/nodejs/no1.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/nodejs/no1.js') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 3b3f8a0..b66940b 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -47,12 +47,12 @@ function toISO8601(date) { ].join(''); } function createWayBboxQuery(key, value, left, bottom, right, top) { - return "SELECT id,tstamp,version,changeset_id, nodes, user_id, hstore_to_array(tags) as tags FROM ways WHERE (tags @> hstore('" + key + "','" + value + "') AND linestring && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + + return "SELECT id,tstamp,version,changeset_id, nodes, user_id, hstore_to_array(tags) as tags FROM ways WHERE (tags @> hstore('" + key + "','" + value + "') AND linestring && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; } function createNodeBboxQuery(key, value, left, bottom, right, top) { - return "SELECT id, user_id,tstamp,version,changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE (tags @> hstore('" + key + "','" + value + "') AND geom && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + + return "SELECT id, user_id,tstamp,version,changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE (tags @> hstore('" + key + "','" + value + "') AND geom && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; } @@ -71,14 +71,14 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { //console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); var success = false; var query = client.query(createNodeBboxQuery(key, value, left, bottom, right, top)); - + query.on('error', function(err) { - + console.log(err); res.writeHead(404,{}); res.end('\n'); }); - + query.on('end', function() { //console.log("end event\n"); if(success) { @@ -92,17 +92,17 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { //perhaps write 404? is error also raised? } }); - + query.on('row', function(row) { - + if(!success) { success = true; res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(""); } - + console.log(row); - + var node = builder.begin('node') .att('id', row.id) .att('timetamp', toISO8601(row.tstamp)) @@ -116,7 +116,7 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { node.ele('tag') .att('k',escape(temp[x])) .att('v',escape(temp[x+1])); - } + } res.write(builder.toString({ pretty: true })); }); @@ -150,13 +150,13 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { var success = false; //console.log(createWayBboxQuery(key, value, left, bottom, right, top)); var query = client.query(createWayBboxQuery(key, value, left, bottom, right, top)); - + query.on('error', function(err) { console.log(err); res.writeHead(404,{}); res.end(); }); - + query.on('end', function() { if(success) { if(count == 0) { @@ -172,7 +172,7 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { //perhaps write 404? } }); - + query.on('row', function(row) { if(!success) { success = true; @@ -208,10 +208,10 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { } res.write(builder.toString({pretty:'true'})); }); - + //console.log(createNodesForWayQuery(row.nodes)); } - + var way = builder.begin('way') .att('id', row.id) .att('timetamp', toISO8601(row.tstamp)) @@ -224,19 +224,19 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { .att('k',escape(temp[x])) .att('v',escape(temp[x+1])); } - + var temp = row.nodes.replace("{","").replace("}","").split(","); for(var x=0;x