aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2011-01-21 18:25:32 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2011-01-21 18:25:32 +0100
commit70a449fe462386009d345fb92efe3534fd99d12e (patch)
tree803317a7080649857443c2fe20f23f0116cc0f11 /src
parent0ea9b3200dacf84a22be03bbcf62915ff28debf3 (diff)
parentdda92dc140c951b7511937e5927e783f47b347cf (diff)
downloadosm-xapi-70a449fe462386009d345fb92efe3534fd99d12e.tar.gz
osm-xapi-70a449fe462386009d345fb92efe3534fd99d12e.tar.xz
osm-xapi-70a449fe462386009d345fb92efe3534fd99d12e.zip
Merge remote branch 'upstream/master'
Conflicts: src/nodejs/no1.js
Diffstat (limited to 'src')
-rwxr-xr-xsrc/curl/requests.sh3
-rw-r--r--src/nodejs/no1.js275
-rw-r--r--src/nodejs/xmlGenerator.js71
3 files changed, 168 insertions, 181 deletions
diff --git a/src/curl/requests.sh b/src/curl/requests.sh
new file mode 100755
index 0000000..0c556cc
--- /dev/null
+++ b/src/curl/requests.sh
@@ -0,0 +1,3 @@
+curl -i -g localhost:8080/api/node[bbox=12,52,13,53]
+curl -i -g localhost:8080/api/node[amenity=pub][bbox=12,52,13,53]
+curl -i -g localhost:8080/api/node[amenity=pub]
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js
index 94e475d..9e59bcf 100644
--- a/src/nodejs/no1.js
+++ b/src/nodejs/no1.js
@@ -1,51 +1,28 @@
var clutch = require('clutch');
var pg = require('pg');
-var builder = require('xmlbuilder');
+var xmlGenerator = require('./xmlGenerator.js');
// load config
var config = require('./config.json');
-process.argv.forEach(
- function (val,index, array){
+process.argv.forEach(function (val,index, array){
if(val=="-c"){
- path = array[index+1];
- if( path[0] != '/'){
- path = __dirname + '/' + path;
- }
- config = require(path);
+ path = array[index+1];
+ if( path[0] != '/'){
+ path = __dirname + '/' + 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 logger = log4js.getLogger('global');
-logger.setLevel('ALL');
-
-logger.info("server starting...");
-
-function toISO8601(date) {
- //2007-03-31T00:09:22+01:00
- var pad_two = function(n) {
- return (n < 10 ? '0' : '') + n;
- };
+var log = log4js.getLogger('global');
+log.setLevel('ALL');
- return [
- date.getUTCFullYear(),
- '-',
- pad_two(date.getUTCMonth() + 1),
- '-',
- pad_two(date.getUTCDate()),
- 'T',
- pad_two(date.getUTCHours()),
- ':',
- pad_two(date.getUTCMinutes()),
- ':',
- pad_two(date.getUTCSeconds()),
- '+01:00' //FIX ME
- ].join('');
-}
+log.info("server starting...");
function createWayBboxQuery(key, value, left, bottom, right, top) {
return {
@@ -95,183 +72,119 @@ function nodeWorldHandler(req, res, key, value) {
}
function nodeBboxHandler(req, res, key, value, left, bottom, right, top) {
- console.log("nodeBboxHandler");
+ log.error("nodeBboxHandler");
db_connect(res, function(client) {
- console.log(createNodeBboxQuery(key, value, left, bottom, right, top));
- var success = false;
- var query = client.query(createNodeBboxQuery(key, value, left, bottom, right, top));
+ log.info(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('error', function(err) {
+ log.error(err);
+ res.writeHead(404,{});
+ res.end('\n');
+ });
- query.on('end', function() {
- //console.log("end event\n");
- if(success) {
+ query.on('end', function() {
+ //console.log("end event\n");
+ if(success) {
res.write("</xml>");
res.end();
- }
- else {
+ }
+ else {
//empty response
res.writeHead(404,{});
res.end();
//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("<xml>");
- }
-
- console.log(row);
-
- var node = builder.begin('node')
- .att('id', row.id)
- .att('timetamp', toISO8601(row.tstamp))
- .att('version', row.version)
- .att('changeset', row.changeset_id)
- .att('lat', row.lat)
- .att('lon', row.lon);
+ }
+ });
- res.write(createXmlFromRow(row));
- });
+ query.on('row', function(row) {
+ if(!success) {
+ success = true;
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write("<xml>");
+ }
+ res.write(xmlGenerator.createNode(row));
+ });
});
}
-function createXmlFromRow(row) {
- console.log(row);
- var node = builder.begin('node')
- .att('id', row.id)
- .att('timetamp', toISO8601(row.tstamp))
- .att('version', row.version)
- .att('changeset', row.changeset_id)
- .att('lat', row.lat)
- .att('lon', row.lon);
- if(row.tags != '{}') {
- var temp = row.tags.replace("{","").replace("}","").split(",");
- for(var x=0;x<temp.length;x=x+2)
- node.ele('tag')
- .att('k',escape(temp[x]))
- .att('v',escape(temp[x+1]));
- }
- return builder.toString({ pretty: true });
-}
-
function wayWorldHandler(req, res, key, value) {
res.writeHead(200, {'Content-Type': 'text/plain'});
}
function connectionError(err, res) {
- console.log(err);
- console.log("foobar");
+ log.error(err);
+ log.fatal("connectionError not implemented");
}
function db_connect(res, callback) {
pg.connect(connectionString, function(err, client) {
- if(err) {
- console.log(err['message']);
+ if(err) {
+ log.error('message');
res.writeHead(404,{});
res.end();
- } else {
- console.log("db connection was successfull");
+ } else {
+ log.info("db connection was successfull");
callback(client);
- }
- });
+ }
+ });
}
function wayBboxHandler(req, res, key, value, left, bottom, right, top) {
db_connect(res, function(client) {
- var count = 0;
- var success = false;
- //console.log(createWayBboxQuery(key, value, left, bottom, right, top));
- var query = client.query(createWayBboxQuery(key, value, left, bottom, right, top));
+ var count = 0;
+ 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('error', function(err) {
+ log.error(err);
+ res.writeHead(404,{});
+ res.end();
+ });
- query.on('end', function() {
- if(success) {
+ query.on('end', function() {
+ if(success) {
if(count == 0) {
- res.write("</xml>");
- res.end();
+ res.write("</xml>");
+ res.end();
}
//res.write("</xml>");
//res.end(); //problem!!!
- }
- else {
+ }
+ else {
res.writeHead(404,{});
res.end();
//perhaps write 404?
- }
- });
-
- query.on('row', function(row) {
- if(!success) {
- success = true;
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.write("<xml>");
- }
- //console.log(row);
- if(row.nodes != '{}') {
- count++;
- var subquery = client.query(createNodesForWayQuery(row.nodes));
- subquery.on('error',function(err) {});
- subquery.on('end', function() {
- count--;
- if(count==0)
- res.write("</xml>");
- res.end();
- });
- subquery.on('row', function(row) {
- console.log(row);
- var node = builder.begin('node')
- .att('id', row.id)
- .att('timetamp', toISO8601(row.tstamp))
- .att('version', row.version)
- .att('changeset', row.changeset_id)
- .att('lat', row.lat)
- .att('lon', row.lon);
- if(row.tags != '{}') {
- var temp = row.tags.replace("{","").replace("}","").split(",");
- for(var x=0;x<temp.length;x=x+2)
- node.ele('tag')
- .att('k',escape(temp[x]))
- .att('v',escape(temp[x+1]));
- }
- 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))
- .att('version', row.version)
- .att('changeset', row.changeset_id);
- if(row.tags != '{}') {
- var temp = row.tags.replace("{","").replace("}","").split(",");
- for(var x=0;x<temp.length;x=x+2)
- way.ele('tag')
- .att('k',escape(temp[x]))
- .att('v',escape(temp[x+1]));
- }
-
- var temp = row.nodes.replace("{","").replace("}","").split(",");
- for(var x=0;x<temp.length;x++)
- way.ele('nd')
- .att('ref',temp[x]);
+ query.on('row', function(row) {
+ if(!success) {
+ success = true;
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write("<xml>");
+ }
+ //console.log(row);
+ if(row.nodes != '{}') {
+ count++;
+ var subquery = client.query(createNodesForWayQuery(row.nodes));
+ subquery.on('error',function(err) {});
+ subquery.on('end', function() {
+ count--;
+ if(count==0)
+ res.write("</xml>");
+ res.end();
+ });
+ subquery.on('row', function(row) {
+ res.write(xmlGenerator.createNode(row));
+ });
- res.write(builder.toString({pretty:'true'}));
- });
+ //console.log(createNodesForWayQuery(row.nodes));
+ }
+ res.write(xmlGenerator.createWay(row));
+ });
});
}
@@ -285,18 +198,18 @@ 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+(?:\\.\\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+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',wayBboxHandler],
- ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler],
- //['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler],
-]);
+ //['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+(?:\\.\\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+)?),(\\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(config.port, config.host);
-logger.info("Started server at " + config.host + ":" + config.port );
+log.info("Started server at " + config.host + ":" + config.port );
diff --git a/src/nodejs/xmlGenerator.js b/src/nodejs/xmlGenerator.js
new file mode 100644
index 0000000..e982e18
--- /dev/null
+++ b/src/nodejs/xmlGenerator.js
@@ -0,0 +1,71 @@
+var builder = require('xmlbuilder');
+//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('xmlGenerator');
+log.setLevel('ALL');
+
+
+exports.createNode = function (row) {
+ log.debug(row);
+ var node = builder.begin('node')
+ .att('id', row.id)
+ .att('timestamp', toISO8601(row.tstamp))
+ .att('version', row.version)
+ .att('changeset', row.changeset_id)
+ .att('lat', row.lat)
+ .att('lon', row.lon);
+ if(row.tags != '{}') {
+ var temp = row.tags.replace("{","").replace("}","").split(",");
+ for(var x=0;x<temp.length;x=x+2)
+ node.ele('tag')
+ .att('k',escape(temp[x]))
+ .att('v',escape(temp[x+1]));
+ }
+ return builder.toString({ pretty: true });
+}
+
+exports.createWay = function (row) {
+ var way = builder.begin('way')
+ .att('id', row.id)
+ .att('timestamp', toISO8601(row.tstamp))
+ .att('version', row.version)
+ .att('changeset', row.changeset_id);
+ if(row.tags != '{}') {
+ var temp = row.tags.replace("{","").replace("}","").split(",");
+ for(var x=0;x<temp.length;x=x+2)
+ way.ele('tag')
+ .att('k',escape(temp[x]))
+ .att('v',escape(temp[x+1]));
+ }
+
+ var temp = row.nodes.replace("{","").replace("}","").split(",");
+ for(var x=0;x<temp.length;x++) {
+ way.ele('nd')
+ .att('ref',temp[x]);
+ }
+ return builder.toString({pretty:'true'});
+}
+
+function toISO8601(date) {
+ //2007-03-31T00:09:22+01:00
+ var pad_two = function(n) {
+ return (n < 10 ? '0' : '') + n;
+ };
+
+ return [
+ date.getUTCFullYear(),
+ '-',
+ pad_two(date.getUTCMonth() + 1),
+ '-',
+ pad_two(date.getUTCDate()),
+ 'T',
+ pad_two(date.getUTCHours()),
+ ':',
+ pad_two(date.getUTCMinutes()),
+ ':',
+ pad_two(date.getUTCSeconds()),
+ '+01:00' //FIX ME
+ ].join('');
+}