From f9286a5ab887b2228bd422ed90a2c24addf0243b Mon Sep 17 00:00:00 2001 From: slomo Date: Fri, 21 Jan 2011 15:00:12 +0100 Subject: added new config load2 --- src/nodejs/no1.js | 14 +++++++ src/nodejs/no2.js | 106 ---------------------------------------------------- src/nodejs/start.js | 105 --------------------------------------------------- 3 files changed, 14 insertions(+), 211 deletions(-) delete mode 100644 src/nodejs/no2.js delete mode 100644 src/nodejs/start.js (limited to 'src/nodejs') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 1132a11..fdf40e3 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -5,7 +5,21 @@ var builder = require('xmlbuilder'); var config = require('./config.json'); +// load config +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); diff --git a/src/nodejs/no2.js b/src/nodejs/no2.js deleted file mode 100644 index 233b5e1..0000000 --- a/src/nodejs/no2.js +++ /dev/null @@ -1,106 +0,0 @@ -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 deleted file mode 100644 index b92c757..0000000 --- a/src/nodejs/start.js +++ /dev/null @@ -1,105 +0,0 @@ -// imports -var http = require('http'), - querystring = require('querystring'), - pg = require('pg'), - url = require('url'), - querystring = require('querystring'); - -// config -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]; - } - return "SELECT * FROM \"" + table + "\" " + filter + ";"; -} - - -RegExp.escape= function(s) { - return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') -}; - - -var PATH_PREFIX = '/api/'; -var base_url_re = new RegExp('^' + RegExp.escape(PATH_PREFIX) + - '(node|way|relation|\\*)(.*)$') -var filter_re = /\[(\w+=[^\]]+)\]/g; - - -http.createServer(function (req, res) { - - if (!base_url_re.test(req.url)) { - res.writeHead(404, {'Content-Type': 'text/plain; charset=utf8'}); - res.end('Not Found\n'); - } - 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; - - for(i=0;i