Added cors middleware, decoupled query and job controllers from its dependencies

This commit is contained in:
Daniel García Aubert 2016-01-21 17:24:48 +01:00
parent 048d2cd0ea
commit 79e626a71e
7 changed files with 48 additions and 43 deletions

View File

@ -21,6 +21,16 @@ var StatsD = require('node-statsd').StatsD;
var _ = require('underscore');
var LRU = require('lru-cache');
var UserDatabaseService = require('./services/user_database_service');
var JobPublisher = require('../batch/job_publisher');
var JobQueue = require('../batch/job_queue');
var UserIndexer = require('../batch/user_indexer');
var JobBackend = require('../batch/job_backend');
var JobCanceller = require('../batch/job_canceller');
var UserDatabaseMetadataService = require('../batch/user_database_metadata_service');
var cors = require('./middlewares/cors');
var GenericController = require('./controllers/generic_controller');
var QueryController = require('./controllers/query_controller');
var JobController = require('./controllers/job_controller');
@ -48,6 +58,7 @@ function App() {
reapIntervalMillis: global.settings.redisReapIntervalMillis
});
// Set default configuration
global.settings.db_pubuser = global.settings.db_pubuser || "publicuser";
global.settings.bufferedRows = global.settings.bufferedRows || 1000;
@ -139,6 +150,10 @@ function App() {
});
}
//
app.use(cors());
// Use step-profiler
if ( global.settings.useProfiler ) {
app.use(function(req, res, next) {
@ -162,13 +177,22 @@ function App() {
// basic routing
var userDatabaseService = new UserDatabaseService(metadataBackend);
var jobQueue = new JobQueue(metadataBackend);
var jobPublisher = new JobPublisher();
var userIndexer = new UserIndexer(metadataBackend);
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
var jobCanceller = new JobCanceller(metadataBackend, userDatabaseMetadataService, jobBackend);
var genericController = new GenericController();
genericController.route(app);
var queryController = new QueryController(metadataBackend, tableCache, statsd_client);
var queryController = new QueryController(userDatabaseService, tableCache, statsd_client);
queryController.route(app);
var jobController = new JobController(metadataBackend, tableCache, statsd_client);
var jobController = new JobController(userDatabaseService, jobBackend, jobCanceller, tableCache, statsd_client);
jobController.route(app);
var cacheStatusController = new CacheStatusController(tableCache);

View File

@ -1,7 +1,5 @@
'use strict';
var setCrossDomain = require('../utils/cross_domain');
function GenericController() {
}
@ -10,7 +8,6 @@ GenericController.prototype.route = function (app) {
};
GenericController.prototype.handleRequest = function(req, res) {
setCrossDomain(res);
res.end();
};

View File

@ -4,33 +4,17 @@ var _ = require('underscore');
var step = require('step');
var assert = require('assert');
var UserDatabaseService = require('../services/user_database_service');
var AuthApi = require('../auth/auth_api');
var JobPublisher = require('../../batch/job_publisher');
var JobQueue = require('../../batch/job_queue');
var UserIndexer = require('../../batch/user_indexer');
var JobBackend = require('../../batch/job_backend');
var JobCanceller = require('../../batch/job_canceller');
var UserDatabaseMetadataService = require('../../batch/user_database_metadata_service');
var CdbRequest = require('../models/cartodb_request');
var handleException = require('../utils/error_handler');
var cdbReq = new CdbRequest();
function JobController(metadataBackend, tableCache, statsd_client) {
var jobQueue = new JobQueue(metadataBackend);
var jobPublisher = new JobPublisher();
var userIndexer = new UserIndexer(metadataBackend);
this.userDatabaseService = new UserDatabaseService(metadataBackend);
this.metadataBackend = metadataBackend;
function JobController(userDatabaseService, jobBackend, jobCanceller, tableCache, statsd_client) {
this.userDatabaseService = userDatabaseService;
this.jobBackend = jobBackend;
this.jobCanceller = jobCanceller;
this.tableCache = tableCache;
this.statsd_client = statsd_client;
this.jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
this.userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
this.jobCanceller = new JobCanceller(this.metadataBackend, this.userDatabaseMetadataService, this.jobBackend);
}
JobController.prototype.route = function (app) {

View File

@ -5,7 +5,6 @@ var step = require('step');
var assert = require('assert');
var PSQL = require('cartodb-psql');
var UserDatabaseService = require('../services/user_database_service');
var AuthApi = require('../auth/auth_api');
var CdbRequest = require('../models/cartodb_request');
@ -15,17 +14,15 @@ var sanitize_filename = require('../utils/filename_sanitizer');
var generateMD5 = require('../utils/md5');
var queryMayWrite = require('../utils/query_may_write');
var getContentDisposition = require('../utils/content_disposition');
var setCrossDomain = require('../utils/cross_domain');
var generateCacheKey = require('../utils/cache_key_generator');
var handleException = require('../utils/error_handler');
var cdbReq = new CdbRequest();
function QueryController(metadataBackend, tableCache, statsd_client) {
this.metadataBackend = metadataBackend;
function QueryController(userDatabaseService, tableCache, statsd_client) {
this.tableCache = tableCache;
this.statsd_client = statsd_client;
this.userDatabaseService = new UserDatabaseService(metadataBackend);
this.userDatabaseService = userDatabaseService;
}
QueryController.prototype.route = function (app) {
@ -230,9 +227,6 @@ QueryController.prototype.handleQuery = function (req, res) {
res.header("Content-Disposition", getContentDisposition(formatter, filename, use_inline));
res.header("Content-Type", formatter.getContentType());
// allow cross site post
setCrossDomain(res);
// set cache headers
var ttl = 31536000; // 1 year time to live by default
var cache_policy = req.query.cache_policy;

16
app/middlewares/cors.js Normal file
View File

@ -0,0 +1,16 @@
'use strict';
module.exports = function cors(extraHeaders) {
return function(req, res, next) {
var baseHeaders = 'X-Requested-With, X-Prototype-Version, X-CSRF-Token';
if(extraHeaders) {
baseHeaders += ', ' + extraHeaders;
}
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', baseHeaders);
next();
};
};

View File

@ -1,6 +0,0 @@
'use strict';
module.exports = function setCrossDomain(res){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, X-Prototype-Version, X-CSRF-Token");
};

View File

@ -2,7 +2,6 @@
var _ = require('underscore');
var PgErrorHandler = require('../postgresql/error_handler');
var setCrossDomain = require('../utils/cross_domain');
// jshint unused: false
module.exports = function handleException(err, res) {
@ -23,9 +22,6 @@ module.exports = function handleException(err, res) {
console.error("EXCEPTION REPORT: " + err.stack);
}
// allow cross site post
setCrossDomain(res);
// Force inline content disposition
res.header("Content-Disposition", 'inline');