1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

chore(modernize): Modernize IndexController

This commit is contained in:
ivaosthu 2018-11-24 12:58:30 +01:00 committed by Ivar Conradi Østhus
parent b870e209a9
commit 2dde9c4b95
5 changed files with 38 additions and 27 deletions

View File

@ -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());

View File

@ -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();
}
},
});

View File

@ -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;

View File

@ -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) => {

View File

@ -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();
}
},
});