From 298297f2c5d1580d5a1af76cb05894b7471bde78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 26 Jul 2019 19:51:06 +0200 Subject: [PATCH] Use ES6 syntax class --- app/controllers/query_controller.js | 86 +++++++++++++++-------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/app/controllers/query_controller.js b/app/controllers/query_controller.js index fffbe93c..9ed85079 100644 --- a/app/controllers/query_controller.js +++ b/app/controllers/query_controller.js @@ -23,45 +23,47 @@ const lastModified = require('../middlewares/last-modified'); const formatter = require('../middlewares/formatter'); const content = require('../middlewares/content'); -function QueryController(metadataBackend, userDatabaseService, statsdClient, userLimitsService) { - this.metadataBackend = metadataBackend; - this.stats = statsdClient; - this.userDatabaseService = userDatabaseService; - this.userLimitsService = userLimitsService; -} +module.exports = class QueryController { + constructor (metadataBackend, userDatabaseService, statsdClient, userLimitsService) { + this.metadataBackend = metadataBackend; + this.stats = statsdClient; + this.userDatabaseService = userDatabaseService; + this.userLimitsService = userLimitsService; + } -QueryController.prototype.route = function (app) { - const { base_url } = global.settings; - const forceToBeMaster = false; + route (app) { + const { base_url } = global.settings; + const forceToBeMaster = false; - const queryMiddlewares = () => { - return [ - bodyParserMiddleware(), - initializeProfilerMiddleware('query'), - userMiddleware(this.metadataBackend), - rateLimitsMiddleware(this.userLimitsService, RATE_LIMIT_ENDPOINTS_GROUPS.QUERY), - authorizationMiddleware(this.metadataBackend, forceToBeMaster), - connectionParamsMiddleware(this.userDatabaseService), - timeoutLimitsMiddleware(this.metadataBackend), - parameters({ strategy: 'query' }), - logMiddleware(logMiddleware.TYPES.QUERY), - cancelOnClientAbort(), - affectedTables(), - accessValidator(), - queryMayWrite(), - cacheControl(), - cacheChannel(), - surrogateKey(), - lastModified(), - formatter(), - content(), - handleQuery({ stats: this.stats }), - errorMiddleware() - ]; - }; + const queryMiddlewares = () => { + return [ + bodyParserMiddleware(), + initializeProfilerMiddleware('query'), + userMiddleware(this.metadataBackend), + rateLimitsMiddleware(this.userLimitsService, RATE_LIMIT_ENDPOINTS_GROUPS.QUERY), + authorizationMiddleware(this.metadataBackend, forceToBeMaster), + connectionParamsMiddleware(this.userDatabaseService), + timeoutLimitsMiddleware(this.metadataBackend), + parameters({ strategy: 'query' }), + logMiddleware(logMiddleware.TYPES.QUERY), + cancelOnClientAbort(), + affectedTables(), + accessValidator(), + queryMayWrite(), + cacheControl(), + cacheChannel(), + surrogateKey(), + lastModified(), + formatter(), + content(), + handleQuery({ stats: this.stats }), + errorMiddleware() + ]; + }; - app.all(`${base_url}/sql`, queryMiddlewares()); - app.all(`${base_url}/sql.:f`, queryMiddlewares()); + app.all(`${base_url}/sql`, queryMiddlewares()); + app.all(`${base_url}/sql.:f`, queryMiddlewares()); + } }; function handleQuery ({ stats } = {}) { @@ -110,16 +112,18 @@ function handleQuery ({ stats } = {}) { next(err); } - if ( req.profiler ) { + if (req.profiler) { req.profiler.sendStats(); } - if (statsdClient) { - if ( err ) { + + if (stats) { + if (err) { stats.increment('sqlapi.query.error'); } else { stats.increment('sqlapi.query.success'); } } + }); } catch (err) { next(err); @@ -129,6 +133,4 @@ function handleQuery ({ stats } = {}) { } } }; -}; - -module.exports = QueryController; +}