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 +++++++++ src/nodejs/no2.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nodejs/start.js | 79 ++++++++++++++++++--------------------- 3 files changed, 159 insertions(+), 43 deletions(-) create mode 100644 src/nodejs/no1.js create mode 100644 src/nodejs/no2.js (limited to 'src/nodejs') 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'); diff --git a/src/nodejs/no2.js b/src/nodejs/no2.js new file mode 100644 index 0000000..233b5e1 --- /dev/null +++ b/src/nodejs/no2.js @@ -0,0 +1,106 @@ +var users = [ + { name: 'tj' }, + { name: 'tim' } +]; + +function user(app) { + app.resource('/.:format?', { + 'get' : function(req, res, next) { + switch (req.params.format) { + case 'json': + var body = JSON.stringify(users); + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Content-Length': body.length + }); + res.end(body); + break; + default: + var body = ''; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body); + } + } + }); + + app.resource('/:id.:format', { + 'get' : function(req, res, next) { + var user = users[req.params.id]; + if (user && req.params.format === 'json') { + user = JSON.stringify(user); + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Content-Length': user.length + }); + res.end(user); + } + else { + // When true is passed, provide control + // back to middleware, skipping route + // match attemps + next(true); + } + } + }) + + app.resource('/\\[:id/:op?', { + 'get' : function(req, res) { + var body = users[req.params.id] + ? users[req.params.id].name + : 'User ' + req.params.id + ' does not exist'; + body = (req.params.op || 'view') + 'ing ' + body; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body, 'utf8'); + } + }) +} + + +function main(app) { + app.resource('/', { + 'get' : function(req, res) { + var examples = [ + '/users', + '/users.json', + '/users/0 (or /users/0/view)', + '/users/0/edit', + '/users/0.json' + ]; + var body = 'Visit one of the following: '; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body, 'utf8'); + } + }); +} + + +var connect = require('connect'); +var resource = require('resource-router'); + +var server = connect.createServer( + connect.logger({ buffer: true }), + connect.cache(), + connect.gzip() + ); + +server.use('/users', resource(user)); +server.use(resource(main)); +server.listen(3000); +console.log('Connect server listening on port 3000'); diff --git a/src/nodejs/start.js b/src/nodejs/start.js index c664cfe..b92c757 100644 --- a/src/nodejs/start.js +++ b/src/nodejs/start.js @@ -6,52 +6,45 @@ var http = require('http'), querystring = require('querystring'); // config -var connectionString = "pg://user:pass@localhost/xapi" - - -function getDataBaseResult(tag,bbox,res) { - pg.connect(connectionString,function(err,client) { - var the_result; - - if (err) { - console.log(err); - } - else { - client.query(createQuery(tag,bbox),function(err,result) { - if (err) { - console.log(err); - } - else { - console.log(result); - res.write(result.rows); - res.end("\n"); - } - }); - } - }); +var connectionString = "pg://yves:test@localhost/xapi" + + +function getDataBaseResult(tag,bbox,res){ +pg.connect(connectionString,function(err,client){ + var the_result; + if(err){ + console.log(err); + } else { + client.query(createQuery(tag,bbox),function(err,result){ + if (err) { + console.log(err); + } else { + console.log(result); + res.write(result.rows); + res.end("/n"); + } + + }); + } +}); } function createQuery(tag,bbox){ // FIXME: validate - // var table = tag[0] + "#" + tag[1]; - // var filter = ""; - - // // input validation - // for(i=0;i " + bbox[2] + " AND latitude < " + bbox[3]; - // } + var table = tag[0] + "#" + tag[1]; + var filter = ""; + // input validation + for(i=0;i '\"amenity\"=>\"pub\"' AND \ - lseg(linestring) @ box('(13.0882097323,52.3418234221)'::point,'(13.7606105539,52.6697240587)'::point));" + if(bbox){ + filter = "WHERE longitude > " + bbox[0] + " AND longitude < " + bbox[1] + + " AND latitude > " + bbox[2] + " AND latitude < " + bbox[3]; + } + return "SELECT * FROM \"" + table + "\" " + filter + ";"; } @@ -74,13 +67,13 @@ http.createServer(function (req, res) { } base_url_re.exec(req.url); var type = RegExp.$1, url_rest = querystring.unescape(RegExp.$2); - + var filters = []; while (v = filter_re.exec(url_rest)) { filters.push(v[1]); } console.log(filters); - + var tag; var bbox; @@ -97,10 +90,10 @@ http.createServer(function (req, res) { console.log(tag); console.log(bbox); - getDataBaseResult(tag,bbox,res); + getDataBaseResult(tag,bbox,res); res.writeHead(200, {'Content-Type': 'text/plain; charset=utf8', }); - + res.write('URL was: ' + req.url + '\n'); res.write('type: ' + type + '\n'); res.write('filters:\n'); -- 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') 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