diff --git a/app.js b/app.js index c6f01480..86ebbf17 100644 --- a/app.js +++ b/app.js @@ -25,4 +25,5 @@ var env = require(__dirname + '/config/environments/' + ENV) _.extend(global.settings, env); // kick off controller -require(global.settings.app_root + '/app/controllers/app'); +var app = require(global.settings.app_root + '/app/controllers/app'); +app.listen(global.settings.node_port); \ No newline at end of file diff --git a/app/controllers/app.js b/app/controllers/app.js index 28203ca0..02ac8558 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -1,4 +1,8 @@ -var app = require('express').createServer() +var express= require('express') + , app = express.createServer( + //TODO: make logs async background + 1min or so + //express.logger('\033[90m:method\033[0m \033[36m:url\033[0m \033[90m:status :response-timems -> :res[Content-Type]\033[0m') + ) , Step = require(global.settings.app_root + '/lib/step') , oAuth = require(global.settings.app_root + '/app/models/oauth') , PSQL = require(global.settings.app_root + '/app/models/psql') @@ -35,7 +39,7 @@ app.get('/v1/', function(req, res){ var pg; Step( - function getUser() { + function getUser() { oAuth.authorize(req, this); }, function querySql(err, user_id){ @@ -45,7 +49,6 @@ app.get('/v1/', function(req, res){ }, function packageResults(err, result){ if (err) throw err; - //pg.end(); //TODO: Fix this - we should use a proper generic pool res.send(result.rows); }, function exceptionHandle(err, result){ @@ -62,5 +65,7 @@ function handleException(res, err){ res.send(msg, 400); } -app.listen(global.settings.node_port); -//module.exports = app; +//app.listen(global.settings.node_port); + +// Think of putting it behind a cluster in production +module.exports = app; diff --git a/app/models/oauth.js b/app/models/oauth.js index 16a8d727..b920959d 100644 --- a/app/models/oauth.js +++ b/app/models/oauth.js @@ -46,4 +46,4 @@ var oAuth = function(){ }(); module.exports = oAuth; - \ No newline at end of file + diff --git a/cluster.js b/cluster.js index 637ace1d..093d548e 100755 --- a/cluster.js +++ b/cluster.js @@ -1,13 +1,36 @@ -// #! /Users/fernando/local/node/bin/node -// -// var cluster = require('cluster'); -// -// cluster('./app') -// .use(cluster.logger('logs')) -// .set('workers', 2) -// .use(cluster.stats()) -// .use(cluster.pidfiles('pids')) -// .use(cluster.cli()) -// .use(cluster.repl(8888)) -// .use(cluster.debug()) -// .listen(3000); +#!/usr/bin/env node + +/* +* SQL API loader +* =============== +* +* node app [environment] +* +* environments: [development, test, production] +* +*/ +var _ = require('underscore'); +var cluster = require('cluster'); + +// sanity check arguments +var ENV = process.argv[2] +if (ENV != 'development' && ENV != 'production') { + console.error("\nnode app [environment]"); + console.error("environments: [development, test, production]"); + process.exit(1); +} + +// set Node.js app settings and boot +global.settings = require(__dirname + '/config/settings') +var env = require(__dirname + '/config/environments/' + ENV) +_.extend(global.settings, env); + +cluster('./app/controllers/app') + .use(cluster.logger('logs')) + .set('workers', 2) + .use(cluster.stats()) + .use(cluster.pidfiles('pids')) + .use(cluster.cli()) + .use(cluster.repl(8888)) + .use(cluster.debug()) + .listen(4000); diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index df3cec87..9dd8be80 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -1,5 +1,8 @@ require('../helper'); +// Requires the database and tables setup in config/environments/test.js to exist +// Ensure the user is present in the pgbouncer auth file too + var app = require(global.settings.app_root + '/app/controllers/app') , assert = require('assert'); @@ -12,13 +15,30 @@ module.exports = { body: '{"error":["You must indicate a sql query"]}', status: 400 }); - }// , - // 'GET /v1/ with SQL parameter': function(){ - // assert.response(app, { - // url: '/v1/?sql=bla', - // method: 'GET' - // },{ - // status: 200 - // }); - // } + }, + 'GET /v1/ with SQL parameter on SELECT only. No oAuth included ': function(){ + assert.response(app, { + url: '/v1/?sql=SELECT%20*%20FROM%20test_table&database=cartodb_test_user_1_db', + method: 'GET' + },{ + status: 200 + }); + }, + 'GET /v1/ with SQL parameter on SELECT only. oAuth used ': function(){ + assert.response(app, { + url: '/v1/?sql=SELECT%20*%20FROM%20test_table&oauth_token=1', + method: 'GET' + },{ + status: 200 + }); + }, + 'GET /v1/ with SQL parameter on INSERT only. oAuth used ': function(){ + assert.response(app, { + url: "/v1/?sql=INSERT%20INTO%20test_table%20(id)%20VALUES%20(1)&oauth_token=1", + method: 'GET' + },{ + status: 200 + }); + } + };