diff --git a/lib/app.js b/lib/app.js index f8b554154c..c1b30f941c 100644 --- a/lib/app.js +++ b/lib/app.js @@ -4,7 +4,7 @@ const express = require('express'); const compression = require('compression'); const favicon = require('serve-favicon'); const cookieParser = require('cookie-parser'); -const routes = require('./routes'); +const IndexRouter = require('./routes'); const path = require('path'); const errorHandler = require('errorhandler'); const unleashSession = require('./middleware/session'); @@ -49,11 +49,11 @@ module.exports = function(config) { } // Setup API routes - const middleware = routes.router(config); + const middleware = new IndexRouter(config); if (!middleware) { throw new Error('Routes invalid'); } - app.use(`${baseUriPath}/`, middleware); + app.use(`${baseUriPath}/`, middleware.router()); if (process.env.NODE_ENV !== 'production') { app.use(errorHandler()); diff --git a/lib/app.test.js b/lib/app.test.js index 3b3717e2bc..0a179385c2 100644 --- a/lib/app.test.js +++ b/lib/app.test.js @@ -4,8 +4,10 @@ const { test } = require('ava'); const express = require('express'); const proxyquire = require('proxyquire'); const getApp = proxyquire('./app', { - './routes': { - router: () => express.Router(), + './routes': class Index { + router() { + return express.Router(); + } }, }); diff --git a/lib/routes/index.js b/lib/routes/index.js index 2d5a8bf102..a941d1b1a8 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -13,13 +13,27 @@ const clientFeatures = require('./client-api/feature.js'); const HealthCheckController = require('./health-check'); const BackstageController = require('./backstage.js'); -exports.router = function(config) { - const router = Router(); +class IndexRouter { + constructor(config) { + const router = Router(); + this._router = router; - router.use('/health', new HealthCheckController(config).router()); - router.use('/internal-backstage', new BackstageController(config).router()); + router.use('/health', new HealthCheckController(config).router()); + router.use( + '/internal-backstage', + new BackstageController(config).router() + ); + router.get('/api', (req, res) => this.api(req, res)); + router.use('/api/admin', adminApi.router(config)); + router.use('/api/client', clientApi.router(config)); - router.get('/api', (req, res) => { + // legacy support (remove in 4.x) + if (config.enableLegacyRoutes) { + router.use('/api/features', clientFeatures.router(config)); + } + } + + api(req, res) { res.json({ name: 'unleash-server', version, @@ -34,18 +48,11 @@ exports.router = function(config) { }, }, }); - }); - - router.use('/api/admin', adminApi.router(config)); - router.use('/api/client', clientApi.router(config)); - - // legacy support - // $root/features - // $root/client/register - // $root/client/metrics - if (config.enableLegacyRoutes) { - router.use('/api/features', clientFeatures.router(config)); } - return router; -}; + router() { + return this._router; + } +} + +module.exports = IndexRouter; diff --git a/lib/server-impl.js b/lib/server-impl.js index 1f74d5d5fa..656d7bb0c8 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -24,7 +24,7 @@ function createApp(options) { eventBus, logFactory, }, - options, + options ); const app = getApp(config); @@ -36,7 +36,7 @@ function createApp(options) { ); const server = app.listen({ port: options.port, host: options.host }, () => - logger.info(`Unleash started on port ${server.address().port}`), + logger.info(`Unleash started on port ${server.address().port}`) ); return new Promise((resolve, reject) => { diff --git a/lib/server-impl.test.js b/lib/server-impl.test.js index ae8bf042b2..98d5a8ff5a 100644 --- a/lib/server-impl.test.js +++ b/lib/server-impl.test.js @@ -5,8 +5,10 @@ const proxyquire = require('proxyquire'); const express = require('express'); const getApp = proxyquire('./app', { - './routes': { - router: () => express.Router(), + './routes': class Index { + router() { + return express.Router(); + } }, });