mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	chore(modernize): Modernize IndexController
This commit is contained in:
		
							parent
							
								
									6a36f71a98
								
							
						
					
					
						commit
						00217bfe14
					
				| @ -4,7 +4,7 @@ const express = require('express'); | |||||||
| const compression = require('compression'); | const compression = require('compression'); | ||||||
| const favicon = require('serve-favicon'); | const favicon = require('serve-favicon'); | ||||||
| const cookieParser = require('cookie-parser'); | const cookieParser = require('cookie-parser'); | ||||||
| const routes = require('./routes'); | const IndexRouter = require('./routes'); | ||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const errorHandler = require('errorhandler'); | const errorHandler = require('errorhandler'); | ||||||
| const unleashSession = require('./middleware/session'); | const unleashSession = require('./middleware/session'); | ||||||
| @ -49,11 +49,11 @@ module.exports = function(config) { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Setup API routes
 |     // Setup API routes
 | ||||||
|     const middleware = routes.router(config); |     const middleware = new IndexRouter(config); | ||||||
|     if (!middleware) { |     if (!middleware) { | ||||||
|         throw new Error('Routes invalid'); |         throw new Error('Routes invalid'); | ||||||
|     } |     } | ||||||
|     app.use(`${baseUriPath}/`, middleware); |     app.use(`${baseUriPath}/`, middleware.router()); | ||||||
| 
 | 
 | ||||||
|     if (process.env.NODE_ENV !== 'production') { |     if (process.env.NODE_ENV !== 'production') { | ||||||
|         app.use(errorHandler()); |         app.use(errorHandler()); | ||||||
|  | |||||||
| @ -4,8 +4,10 @@ const { test } = require('ava'); | |||||||
| const express = require('express'); | const express = require('express'); | ||||||
| const proxyquire = require('proxyquire'); | const proxyquire = require('proxyquire'); | ||||||
| const getApp = proxyquire('./app', { | const getApp = proxyquire('./app', { | ||||||
|     './routes': { |     './routes': class Index { | ||||||
|         router: () => express.Router(), |         router() { | ||||||
|  |             return express.Router(); | ||||||
|  |         } | ||||||
|     }, |     }, | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -13,13 +13,27 @@ const clientFeatures = require('./client-api/feature.js'); | |||||||
| const HealthCheckController = require('./health-check'); | const HealthCheckController = require('./health-check'); | ||||||
| const BackstageController = require('./backstage.js'); | const BackstageController = require('./backstage.js'); | ||||||
| 
 | 
 | ||||||
| exports.router = function(config) { | class IndexRouter { | ||||||
|     const router = Router(); |     constructor(config) { | ||||||
|  |         const router = Router(); | ||||||
|  |         this._router = router; | ||||||
| 
 | 
 | ||||||
|     router.use('/health', new HealthCheckController(config).router()); |         router.use('/health', new HealthCheckController(config).router()); | ||||||
|     router.use('/internal-backstage', new BackstageController(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({ |         res.json({ | ||||||
|             name: 'unleash-server', |             name: 'unleash-server', | ||||||
|             version, |             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; | ||||||
|  | |||||||
| @ -24,7 +24,7 @@ function createApp(options) { | |||||||
|             eventBus, |             eventBus, | ||||||
|             logFactory, |             logFactory, | ||||||
|         }, |         }, | ||||||
|         options, |         options | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const app = getApp(config); |     const app = getApp(config); | ||||||
| @ -36,7 +36,7 @@ function createApp(options) { | |||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const server = app.listen({ port: options.port, host: options.host }, () => |     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) => { |     return new Promise((resolve, reject) => { | ||||||
|  | |||||||
| @ -5,8 +5,10 @@ const proxyquire = require('proxyquire'); | |||||||
| const express = require('express'); | const express = require('express'); | ||||||
| 
 | 
 | ||||||
| const getApp = proxyquire('./app', { | const getApp = proxyquire('./app', { | ||||||
|     './routes': { |     './routes': class Index { | ||||||
|         router: () => express.Router(), |         router() { | ||||||
|  |             return express.Router(); | ||||||
|  |         } | ||||||
|     }, |     }, | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user