From cec1c609c04d134f45ee74a81f82c07a19bf11bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 2 Oct 2019 17:35:34 +0200 Subject: [PATCH] Have a default configuration of "routes" when missing environment settings. It prevents errors while deploying (if the new setting is not available yet) --- app/server-options.js | 32 ++++++++++++++++++++++ app/server.js | 4 ++- config/environments/development.js.example | 10 +++++-- config/environments/production.js.example | 10 +++++-- config/environments/staging.js.example | 10 +++++-- config/environments/test.js.example | 10 +++++-- 6 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 app/server-options.js diff --git a/app/server-options.js b/app/server-options.js new file mode 100644 index 00000000..762af298 --- /dev/null +++ b/app/server-options.js @@ -0,0 +1,32 @@ +'use strict' + +module.exports = function getServerOptions () { + const defaults = { + routes: { + // Each entry corresponds with an express' router. + // You must define at least one path. However, middlewares are optional. + api: [{ + // Required: path where other "routers" or "controllers" will be attached to. + paths: [ + // In case the path has a :user param the username will be the one specified in the URL, + // otherwise it will fallback to extract the username from the host header. + '/api/:version', + '/user/:user/api/:version', + ], + // Optional: attach middlewares at the begining of the router + // to perform custom operations. + middlewares: [], + sql: [{ + // Required + paths: [ + '/sql' + ], + // Optional + middlewares: [] + }] + }] + } + }; + + return Object.assign({}, defaults, global.settings); +}; diff --git a/app/server.js b/app/server.js index 04c2257d..3c5d55a7 100644 --- a/app/server.js +++ b/app/server.js @@ -8,6 +8,7 @@ const cartodbRedis = require('cartodb-redis'); const Logger = require('./services/logger'); const ApiRouter = require('./controllers/api-router'); const batchFactory = require('../batch'); +const getServerOptions = require('./server-options'); process.env.PGAPPNAME = process.env.PGAPPNAME || 'cartodb_sqlapi'; @@ -16,6 +17,7 @@ require('./utils/date_to_json'); // jshint maxcomplexity:9 module.exports = function createServer (statsClient) { + const options = getServerOptions(); const app = express(); const redisPool = new RedisPool({ name: 'sql-api', @@ -65,7 +67,7 @@ module.exports = function createServer (statsClient) { statsClient, dataIngestionLogger }); - apiRouter.route(app, global.settings.routes.api); + apiRouter.route(app, options.routes.api); const isBatchProcess = process.argv.indexOf('--no-batch') === -1; diff --git a/config/environments/development.js.example b/config/environments/development.js.example index 9fcc8a4c..343897e0 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -2,14 +2,17 @@ // Disable by using <=0 value. module.exports.gc_interval = 10000; module.exports.routes = { + // Each entry corresponds with an express' router. + // You must define at least one path. However, middlewares are optional. api: [{ + // Required: path where other "routers" or "controllers" will be attached to. paths: [ // In case the path has a :user param the username will be the one specified in the URL, // otherwise it will fallback to extract the username from the host header. '/api/:version', '/user/:user/api/:version', ], - // Attach middlewares at the begining of the req/res cycle + // Optional: attach middlewares at the begining of the router // to perform custom operations. middlewares: [ function noop () { @@ -19,9 +22,12 @@ module.exports.routes = { } ], sql: [{ + // Required paths: [ '/sql' - ] + ], + // Optional + middlewares: [] }] }] }; diff --git a/config/environments/production.js.example b/config/environments/production.js.example index 660f5d1a..6ca0fc8d 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -2,14 +2,17 @@ // Disable by using <=0 value. module.exports.gc_interval = 10000; module.exports.routes = { + // Each entry corresponds with an express' router. + // You must define at least one path. However, middlewares are optional. api: [{ + // Required: path where other "routers" or "controllers" will be attached to. paths: [ // In case the path has a :user param the username will be the one specified in the URL, // otherwise it will fallback to extract the username from the host header. '/api/:version', '/user/:user/api/:version', ], - // Attach middlewares at the begining of the req/res cycle + // Optional: attach middlewares at the begining of the router // to perform custom operations. middlewares: [ function noop () { @@ -19,9 +22,12 @@ module.exports.routes = { } ], sql: [{ + // Required paths: [ '/sql' - ] + ], + // Optional + middlewares: [] }] }] }; diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 5570b270..abbb7e01 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -2,14 +2,17 @@ // Disable by using <=0 value. module.exports.gc_interval = 10000; module.exports.routes = { + // Each entry corresponds with an express' router. + // You must define at least one path. However, middlewares are optional. api: [{ + // Required: path where other "routers" or "controllers" will be attached to. paths: [ // In case the path has a :user param the username will be the one specified in the URL, // otherwise it will fallback to extract the username from the host header. '/api/:version', '/user/:user/api/:version', ], - // Attach middlewares at the begining of the req/res cycle + // Optional: attach middlewares at the begining of the router // to perform custom operations. middlewares: [ function noop () { @@ -19,9 +22,12 @@ module.exports.routes = { } ], sql: [{ + // Required paths: [ '/sql' - ] + ], + // Optional + middlewares: [] }] }] }; diff --git a/config/environments/test.js.example b/config/environments/test.js.example index 91a8f65f..2f8cddf3 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -2,14 +2,17 @@ // Disable by using <=0 value. module.exports.gc_interval = 10000; module.exports.routes = { + // Each entry corresponds with an express' router. + // You must define at least one path. However, middlewares are optional. api: [{ + // Required: path where other "routers" or "controllers" will be attached to. paths: [ // In case the path has a :user param the username will be the one specified in the URL, // otherwise it will fallback to extract the username from the host header. '/api/:version', '/user/:user/api/:version', ], - // Attach middlewares at the begining of the req/res cycle + // Optional: attach middlewares at the begining of the router // to perform custom operations. middlewares: [ function noop () { @@ -19,9 +22,12 @@ module.exports.routes = { } ], sql: [{ + // Required paths: [ '/sql' - ] + ], + // Optional + middlewares: [] }] }] };