2013-03-14 01:41:37 +08:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
2014-01-28 19:04:10 +08:00
|
|
|
var _ = require('underscore');
|
2013-03-14 01:41:37 +08:00
|
|
|
|
|
|
|
var o = function(port, cb) {
|
|
|
|
|
2013-04-04 19:15:50 +08:00
|
|
|
this.queries = [];
|
|
|
|
var that = this;
|
2014-01-30 23:45:40 +08:00
|
|
|
this.requests = [];
|
2014-01-17 00:20:30 +08:00
|
|
|
|
2013-03-14 01:41:37 +08:00
|
|
|
this.sqlapi_server = http.createServer(function(req,res) {
|
2014-01-17 00:20:30 +08:00
|
|
|
//console.log("server got request with method " + req.method);
|
|
|
|
var query;
|
2014-01-30 23:45:40 +08:00
|
|
|
|
|
|
|
that.requests.push(req);
|
|
|
|
|
2014-01-17 00:20:30 +08:00
|
|
|
if ( req.method == 'GET' ) {
|
|
|
|
query = url.parse(req.url, true).query;
|
|
|
|
that.handleQuery(query, res);
|
|
|
|
}
|
|
|
|
else if ( req.method == 'POST') {
|
|
|
|
var data = '';
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
//console.log("GOT Chunk " + chunk);
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
req.on('end', function() {
|
|
|
|
//console.log("Data is: "); console.dir(data);
|
|
|
|
query = JSON.parse(data);
|
|
|
|
//console.log("handleQuery is " + that.handleQuery);
|
|
|
|
that.handleQuery(query, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
that.handleQuery('SQLAPIEmu does not support method' + req.method, res);
|
|
|
|
}
|
2013-03-14 01:41:37 +08:00
|
|
|
}).listen(port, cb);
|
|
|
|
};
|
|
|
|
|
2014-01-17 00:20:30 +08:00
|
|
|
o.prototype.handleQuery = function(query, res) {
|
|
|
|
this.queries.push(query);
|
|
|
|
if ( query.q.match('SQLAPIERROR') ) {
|
|
|
|
res.statusCode = 400;
|
|
|
|
res.write(JSON.stringify({'error':'Some error occurred'}));
|
2014-02-27 17:26:42 +08:00
|
|
|
} else if ( query.q.match('SQLAPINOANSWER') ) {
|
|
|
|
console.log("SQLAPIEmulator will never respond, on request");
|
|
|
|
return;
|
2014-01-17 00:20:30 +08:00
|
|
|
} else if ( query.q.match('EPOCH.* as max') ) {
|
|
|
|
// This is the structure of the known query sent by tiler
|
|
|
|
var row = {
|
|
|
|
'max': 1234567890.123
|
|
|
|
};
|
|
|
|
res.write(JSON.stringify({rows: [ row ]}));
|
|
|
|
} else {
|
2014-01-28 19:04:10 +08:00
|
|
|
if ( query.q.match('_private_') && query.api_key === undefined) {
|
|
|
|
res.statusCode = 403;
|
|
|
|
res.write(JSON.stringify({'error':'forbidden: ' + JSON.stringify(query)}));
|
|
|
|
} else {
|
|
|
|
var qs = JSON.stringify(query);
|
|
|
|
var row = {
|
|
|
|
// This is the structure of the known query sent by tiler
|
|
|
|
'cdb_querytables': '{' + qs + '}',
|
|
|
|
'max': qs
|
|
|
|
};
|
|
|
|
var out_obj = {rows: [ row ]};
|
|
|
|
var out = JSON.stringify(out_obj);
|
|
|
|
res.write(out);
|
|
|
|
}
|
2014-01-17 00:20:30 +08:00
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-14 01:41:37 +08:00
|
|
|
o.prototype.close = function(cb) {
|
|
|
|
this.sqlapi_server.close(cb);
|
|
|
|
};
|
|
|
|
|
2014-01-30 23:45:40 +08:00
|
|
|
o.prototype.getLastRequest = function() {
|
|
|
|
return this.requests.pop();
|
|
|
|
};
|
|
|
|
|
2013-03-14 01:41:37 +08:00
|
|
|
module.exports = o;
|
|
|
|
|