mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	wip wip
This commit is contained in:
		
							parent
							
								
									ddca82213a
								
							
						
					
					
						commit
						85b16cca37
					
				| @ -3,12 +3,17 @@ import FeatureController from '../../features/client-feature-toggles/client-feat | |||||||
| import MetricsController from './metrics'; | import MetricsController from './metrics'; | ||||||
| import RegisterController from './register'; | import RegisterController from './register'; | ||||||
| import { IUnleashConfig, IUnleashServices } from '../../types'; | import { IUnleashConfig, IUnleashServices } from '../../types'; | ||||||
|  | import FeatureStreamController from './stream'; | ||||||
| 
 | 
 | ||||||
| export default class ClientApi extends Controller { | export default class ClientApi extends Controller { | ||||||
|     constructor(config: IUnleashConfig, services: IUnleashServices) { |     constructor(config: IUnleashConfig, services: IUnleashServices) { | ||||||
|         super(config); |         super(config); | ||||||
| 
 | 
 | ||||||
|         this.use('/features', new FeatureController(services, config).router); |         this.use('/features', new FeatureController(services, config).router); | ||||||
|  |         this.use( | ||||||
|  |             '/stream', | ||||||
|  |             new FeatureStreamController(services, config).router, | ||||||
|  |         ); | ||||||
|         this.use('/metrics', new MetricsController(services, config).router); |         this.use('/metrics', new MetricsController(services, config).router); | ||||||
|         this.use('/register', new RegisterController(services, config).router); |         this.use('/register', new RegisterController(services, config).router); | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										80
									
								
								src/lib/routes/client-api/stream.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/lib/routes/client-api/stream.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | |||||||
|  | import { Response } from 'express'; | ||||||
|  | import Controller from '../controller'; | ||||||
|  | import { IUnleashServices } from '../../types'; | ||||||
|  | import { IUnleashConfig } from '../../types/option'; | ||||||
|  | import { Logger } from '../../logger'; | ||||||
|  | import { IAuthRequest, IUser } from '../../server-impl'; | ||||||
|  | import { NONE } from '../../types/permissions'; | ||||||
|  | import ConfigurationRevisionService, { | ||||||
|  |     UPDATE_REVISION, | ||||||
|  | } from '../../features/feature-toggle/configuration-revision-service'; | ||||||
|  | 
 | ||||||
|  | export default class FeatureStreamController extends Controller { | ||||||
|  |     logger: Logger; | ||||||
|  | 
 | ||||||
|  |     clients: any[] = []; | ||||||
|  | 
 | ||||||
|  |     private configurationRevisionService: ConfigurationRevisionService; | ||||||
|  | 
 | ||||||
|  |     constructor( | ||||||
|  |         { | ||||||
|  |             configurationRevisionService, | ||||||
|  |         }: Pick<IUnleashServices, 'configurationRevisionService'>, | ||||||
|  |         config: IUnleashConfig, | ||||||
|  |     ) { | ||||||
|  |         super(config); | ||||||
|  |         this.logger = config.getLogger('/api/client/stream'); | ||||||
|  |         this.configurationRevisionService = configurationRevisionService; | ||||||
|  |         this.configurationRevisionService.on( | ||||||
|  |             UPDATE_REVISION, | ||||||
|  |             this.notifyClients.bind(this), | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         this.route({ | ||||||
|  |             method: 'get', | ||||||
|  |             path: '', | ||||||
|  |             handler: this.stream, | ||||||
|  |             permission: NONE, | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     notifyClients(revisionId: number) { | ||||||
|  |         this.logger.debug('Notifying clients of new revision', revisionId); | ||||||
|  |         this.clients.forEach((client) => { | ||||||
|  |             client.res.write(`data: ${JSON.stringify({ revisionId })}\n\n`); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async stream(req: IAuthRequest, res: Response<void>): Promise<void> { | ||||||
|  |         const headers = { | ||||||
|  |             'Content-Type': 'text/event-stream', | ||||||
|  |             Connection: 'keep-alive', | ||||||
|  |             'Cache-Control': 'no-cache', | ||||||
|  |         }; | ||||||
|  |         res.writeHead(200, headers); | ||||||
|  | 
 | ||||||
|  |         const revisionId = | ||||||
|  |             await this.configurationRevisionService.getMaxRevisionId(); | ||||||
|  | 
 | ||||||
|  |         const toggles = { revisionId }; | ||||||
|  | 
 | ||||||
|  |         const data = `data: ${JSON.stringify(toggles)}\n\n`; | ||||||
|  |         res.write(data); | ||||||
|  | 
 | ||||||
|  |         const clientId = Date.now(); // Use UUIDs for real-world scenarios
 | ||||||
|  | 
 | ||||||
|  |         const newClient = { | ||||||
|  |             id: clientId, | ||||||
|  |             res, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         this.clients.push(newClient); | ||||||
|  | 
 | ||||||
|  |         req.on('close', () => { | ||||||
|  |             console.log(`${clientId} Connection closed`); | ||||||
|  |             this.clients = this.clients.filter( | ||||||
|  |                 (client) => client.id !== clientId, | ||||||
|  |             ); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user