1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

feat: add SSE endpoint for events

This commit is contained in:
Christopher Kolstad 2022-02-25 14:41:06 +01:00
parent 8906bf99ff
commit 71cfee4e89
No known key found for this signature in database
GPG Key ID: 559ACB0E3DB5538A
4 changed files with 42 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import noAuthentication from './middleware/no-authentication';
import secureHeaders from './middleware/secure-headers';
import { loadIndexHTML } from './util/load-index-html';
import { clientEvents } from './types/events';
export default async function getApp(
config: IUnleashConfig,
@ -136,6 +137,22 @@ export default async function getApp(
res.send(indexHTML);
});
if (config.enableEventStream) {
console.log('Enabled, configuring events');
app.get(`${baseUriPath}/api/client/events`, (req, res) => {
res.set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
clientEvents.forEach((e) => {
stores.eventStore.on(e, (data) => {
res.write(`event: message\n`);
res.write(`data: ${JSON.stringify(data)}\n\n`);
});
});
});
}
app.get(`${baseUriPath}/*`, (req, res) => {
if (req.path.startsWith(`${baseUriPath}/api`)) {
res.status(404).send({ message: '404 - Not found' });
@ -144,5 +161,6 @@ export default async function getApp(
res.send(indexHTML);
});
return app;
}

View File

@ -311,6 +311,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
eventHook: options.eventHook,
enterpriseVersion: options.enterpriseVersion,
eventBus: new EventEmitter(),
enableEventStream: safeBoolean(process.env.ENABLE_EVENT_STREAM, false),
};
}

View File

@ -63,6 +63,28 @@ export const ENVIRONMENT_IMPORT = 'environment-import';
export const CLIENT_METRICS = 'client-metrics';
export const clientEvents = [
FEATURE_UPDATED,
FEATURE_ARCHIVED,
FEATURE_CREATED,
FEATURE_DELETED,
FEATURE_ENVIRONMENT_ENABLED,
FEATURE_ENVIRONMENT_DISABLED,
FEATURE_METADATA_UPDATED,
FEATURE_PROJECT_CHANGE,
FEATURE_REVIVED,
FEATURE_STALE_OFF,
FEATURE_STALE_ON,
STRATEGY_CREATED,
STRATEGY_DELETED,
STRATEGY_UPDATED,
STRATEGY_DEPRECATED,
STRATEGY_REACTIVATED,
FEATURE_STRATEGY_ADD,
FEATURE_STRATEGY_REMOVE,
FEATURE_STRATEGY_UPDATE,
];
export interface IBaseEvent {
type: string;
createdBy: string;

View File

@ -158,4 +158,5 @@ export interface IUnleashConfig {
enterpriseVersion?: string;
eventBus: EventEmitter;
disableLegacyFeaturesApi?: boolean;
enableEventStream?: boolean;
}