diff --git a/app/controllers/app.js b/app/controllers/app.js index 5db65bf7..3eacda75 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -35,7 +35,7 @@ var express = require('express') , ApiKeyAuth = require(global.settings.app_root + '/app/models/apikey_auth') , _ = require('underscore') , LRU = require('lru-cache') - , formats = require(global.settings.app_root + '/app/models/formats') + , formats = require(global.settings.app_root + '/app/models/formats') ; var tableCache = LRU({ @@ -45,9 +45,6 @@ var tableCache = LRU({ maxAge: global.settings.tableCacheMaxAge || 1000*60*10 }); -// Keeps track of what's waiting baking for export -var bakingExports = {}; - app.use(express.bodyParser()); app.enable('jsonp callback'); @@ -139,9 +136,6 @@ function handleQuery(req, res) { skipfields = []; } - // setup step run - var start = new Date().getTime(); - if ( -1 === supportedFormats.indexOf(format) ) throw new Error("Invalid format: " + format); @@ -155,11 +149,13 @@ function handleQuery(req, res) { var authenticated; + var formatter; + // 1. Get database from redis via the username stored in the host header subdomain // 2. Run the request through OAuth to get R/W user id if signed // 3. Get the list of tables affected by the query - // 4. Run query with r/w or public user - // 5. package results and send back + // 4. Setup headers + // 5. Send formatted results back Step( function getDatabaseName() { if (_.isNull(database)) { @@ -205,7 +201,7 @@ function handleQuery(req, res) { pg.query("SELECT CDB_QueryTables($quotesql$" + sql + "$quotesql$)", this); } }, - function queryResult(err, result){ + function setHeaders(err, result){ if (err) throw err; // store explain result in local Cache @@ -239,28 +235,15 @@ function handleQuery(req, res) { } - var f = formats[format] - if(f && !f.is_file) { - sql = formats[format].getQuery(sql, { - gn: gn, - dp: dp, - skipfields: skipfields - }) - } else { - // These format are implemented via OGR2OGR, so we don't - // need to run a query ourselves - return null; - } + if ( ! formats.hasOwnProperty(format) ) throw new Error("Unknown format " + format); + var fClass = formats[format] + formatter = new fClass(); - pg.query(window_sql(sql,limit,offset), this); - }, - function setHeaders(err, result){ - if (err) throw err; // configure headers for given format var use_inline = !requestedFormat && !requestedFilename; res.header("Content-Disposition", getContentDisposition(format, filename, use_inline)); - res.header("Content-Type", getContentType(format)); + res.header("Content-Type", formatter.getContentType()); // allow cross site post setCrossDomain(res); @@ -277,105 +260,29 @@ function handleQuery(req, res) { res.header('Cache-Control', 'no-cache,max-age='+ttl+',must-revalidate,public'); } - return result; }, - function packageResults(err, result){ + function generateFormat(err, result){ if (err) throw err; - if ( result && skipfields.length ){ - for ( var i=0; i