From f9c8e0df93c69ca40e404dbc658f89c6cfbeb7fa Mon Sep 17 00:00:00 2001
From: Philipp Borgers <borgers@mi.fu-berlin.de>
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