diff --git a/.emdaer/docs/notes.md b/.emdaer/docs/notes.md index e18f89f..d681c0b 100644 --- a/.emdaer/docs/notes.md +++ b/.emdaer/docs/notes.md @@ -16,3 +16,4 @@ just meant to save ideas for documentation to process some other time._ - Validate the request bodies before reaching the CMS using the resource schemas. - Filter requests that the CMS should not be bothered with using the resource lists. - Make sure to mention that the _Contenta Redis_ module needs to be enabled. +- Make sure to mention that the /healthckeck is for auto-scaling policies. diff --git a/src/helpers/app.js b/src/helpers/app.js index 67384e4..8b4d8e2 100644 --- a/src/helpers/app.js +++ b/src/helpers/app.js @@ -9,11 +9,12 @@ const bodyParser = require('body-parser'); const config = require('config'); const express = require('express'); -const cmsHost = config.get('cms.host'); - -const cacheControl = require('./cacheControl'); -const errorHandler = require('./errorHandler'); -const proxyHandler = require('./proxyHandler'); +const cacheControl = require('../middlewares/cacheControl'); +const copyToRequestObject = require('../middlewares/copyToRequestObject'); +const customCors = require('../middlewares/customCors'); +const errorHandler = require('../middlewares/errorHandler'); +const healthcheck = require('../routes/healthcheck'); +const proxyHandler = require('../routes/proxyHandler'); const app = express(); app.disable('x-powered-by'); @@ -22,28 +23,16 @@ app.disable('x-powered-by'); app.enable('etag'); app.set('etag', 'strong'); const jsonApiPrefix = `/${_.get(process, 'env.jsonApiPrefix')}`; +const cmsHost = config.get('cms.host'); -// Add headers. -app.use((req, res, next) => { - // Enable CORS. - res.set('Access-Control-Allow-Origin', '*'); - res.set( - 'Access-Control-Allow-Headers', - config.get('cors.headers').join(', ') - ); - - // Set the cmsHost and jsonApiPrefix in the request. - req.cmsHost = cmsHost; - req.jsonApiPrefix = jsonApiPrefix; +// Initialize the request object with valuable information. +app.use(copyToRequestObject({ jsonApiPrefix, cmsHost })); - next(); -}); +// Add headers. +app.use(customCors); // Healthcheck is a special endpoint used for application monitoring. -app.get('/healthcheck', (req, res) => { - res.set('Cache-Control', 'private, max-age=0, no-cache'); - res.json({ meta: { healthcheck: 'good' } }); -}); +app.get('/healthcheck', healthcheck); // Set cache control header. app.use(cacheControl); diff --git a/src/helpers/cacheControl.js b/src/middlewares/cacheControl.js similarity index 100% rename from src/helpers/cacheControl.js rename to src/middlewares/cacheControl.js diff --git a/src/helpers/cacheControl.test.js b/src/middlewares/cacheControl.test.js similarity index 100% rename from src/helpers/cacheControl.test.js rename to src/middlewares/cacheControl.test.js diff --git a/src/middlewares/copyToRequestObject.js b/src/middlewares/copyToRequestObject.js new file mode 100644 index 0000000..5631d83 --- /dev/null +++ b/src/middlewares/copyToRequestObject.js @@ -0,0 +1,20 @@ +// @flow + +import type { ObjectLiteral } from '../../flow/types/common'; + +/** + * Returns a middleware that will copy all of the properties to the request. + * + * This is useful to store variables that will be used in any arbitrary + * middleware down the process. + */ +module.exports = (args: ObjectLiteral) => ( + req: any, + res: any, + next: Function +): void => { + Object.keys(args).forEach(key => { + req[key] = args[key]; + }); + next(); +}; diff --git a/src/middlewares/customCors.js b/src/middlewares/customCors.js new file mode 100644 index 0000000..3cc9523 --- /dev/null +++ b/src/middlewares/customCors.js @@ -0,0 +1,16 @@ +// @flow + +const config = require('config'); + +/** + * A custom implementation of CORS. + */ +module.exports = (req: any, res: any, next: Function): void => { + // Enable CORS. + res.set('Access-Control-Allow-Origin', '*'); + res.set( + 'Access-Control-Allow-Headers', + config.get('cors.headers').join(', ') + ); + next(); +}; diff --git a/src/helpers/errorHandler.js b/src/middlewares/errorHandler.js similarity index 100% rename from src/helpers/errorHandler.js rename to src/middlewares/errorHandler.js diff --git a/src/helpers/errorHandler.test.js b/src/middlewares/errorHandler.test.js similarity index 100% rename from src/helpers/errorHandler.test.js rename to src/middlewares/errorHandler.test.js diff --git a/src/helpers/fallbackToCms.js b/src/middlewares/fallbackToCms.js similarity index 100% rename from src/helpers/fallbackToCms.js rename to src/middlewares/fallbackToCms.js diff --git a/src/routes/healthcheck.js b/src/routes/healthcheck.js new file mode 100644 index 0000000..8def1ad --- /dev/null +++ b/src/routes/healthcheck.js @@ -0,0 +1,11 @@ +// @flow + +/** + * A healthcheck endpoint. + * + * Useful for auto-scaling policies, like EC2. + */ +module.exports = (req: any, res: any): void => { + res.set('Cache-Control', 'private, max-age=0, no-cache'); + res.json({ meta: { healthcheck: 'good' } }); +}; diff --git a/src/helpers/proxyHandler.js b/src/routes/proxyHandler.js similarity index 93% rename from src/helpers/proxyHandler.js rename to src/routes/proxyHandler.js index 2f3e9ab..438829c 100644 --- a/src/helpers/proxyHandler.js +++ b/src/routes/proxyHandler.js @@ -3,7 +3,7 @@ const _ = require('lodash'); const config = require('config'); const logger = require('pino')(); -const fallbackToCms = require('./fallbackToCms'); +const fallbackToCms = require('../middlewares/fallbackToCms'); const { redisGet } = require('../caching/drupalRedis')( _.get(process, 'env.redisPrefix', ''), _.get(process, 'env.redisCidTemplate', ''), diff --git a/src/helpers/proxyHandler.test.js b/src/routes/proxyHandler.test.js similarity index 94% rename from src/helpers/proxyHandler.test.js rename to src/routes/proxyHandler.test.js index 6a010e7..61bef91 100644 --- a/src/helpers/proxyHandler.test.js +++ b/src/routes/proxyHandler.test.js @@ -1,6 +1,6 @@ const proxyHandler = require('./proxyHandler'); -jest.mock('./fallbackToCms'); +jest.mock('../middlewares/fallbackToCms'); jest.mock('../caching/drupalRedis', () => () => ({ redisGet(uri) { switch (uri) { @@ -43,7 +43,7 @@ describe('The proxy middleware', () => { let fallbackToCms; beforeEach(() => { - fallbackToCms = require('./fallbackToCms'); + fallbackToCms = require('../middlewares/fallbackToCms'); }); test('It falls back on cache miss', done => {