2022-09-01 15:26:26 +02:00
|
|
|
import { Response } from 'express';
|
|
|
|
import Controller from '../controller';
|
|
|
|
import { IUnleashConfig, IUnleashServices } from '../../types';
|
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import { NONE } from '../../types/permissions';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2023-02-15 09:13:32 +01:00
|
|
|
import { IAuthRequest, RequestBody } from '../unleash-types';
|
2022-09-01 15:26:26 +02:00
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import {
|
|
|
|
validateEdgeTokensSchema,
|
|
|
|
ValidateEdgeTokensSchema,
|
|
|
|
} from '../../openapi/spec/validate-edge-tokens-schema';
|
2023-02-15 09:13:32 +01:00
|
|
|
import ClientInstanceService from '../../services/client-metrics/instance-service';
|
2022-09-01 15:26:26 +02:00
|
|
|
import EdgeService from '../../services/edge-service';
|
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
2023-02-15 09:13:32 +01:00
|
|
|
import { emptyResponse } from '../../openapi/util/standard-responses';
|
|
|
|
import { BulkMetricsSchema } from '../../openapi/spec/bulk-metrics-schema';
|
|
|
|
import ClientMetricsServiceV2 from '../../services/client-metrics/metrics-service-v2';
|
2022-09-01 15:26:26 +02:00
|
|
|
|
|
|
|
export default class EdgeController extends Controller {
|
|
|
|
private readonly logger: Logger;
|
|
|
|
|
|
|
|
private edgeService: EdgeService;
|
|
|
|
|
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2023-02-15 09:13:32 +01:00
|
|
|
private metricsV2: ClientMetricsServiceV2;
|
|
|
|
|
|
|
|
private clientInstanceService: ClientInstanceService;
|
|
|
|
|
2022-09-01 15:26:26 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{
|
|
|
|
edgeService,
|
|
|
|
openApiService,
|
2023-02-15 09:13:32 +01:00
|
|
|
clientMetricsServiceV2,
|
|
|
|
clientInstanceService,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashServices,
|
|
|
|
| 'edgeService'
|
|
|
|
| 'openApiService'
|
|
|
|
| 'clientMetricsServiceV2'
|
|
|
|
| 'clientInstanceService'
|
|
|
|
>,
|
2022-09-01 15:26:26 +02:00
|
|
|
) {
|
|
|
|
super(config);
|
|
|
|
this.logger = config.getLogger('edge-api/index.ts');
|
|
|
|
this.edgeService = edgeService;
|
|
|
|
this.openApiService = openApiService;
|
2023-02-15 09:13:32 +01:00
|
|
|
this.metricsV2 = clientMetricsServiceV2;
|
|
|
|
this.clientInstanceService = clientInstanceService;
|
2022-09-01 15:26:26 +02:00
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/validate',
|
|
|
|
handler: this.getValidTokens,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
this.openApiService.validPath({
|
|
|
|
tags: ['Edge'],
|
|
|
|
operationId: 'getValidTokens',
|
|
|
|
requestBody: createRequestSchema(
|
|
|
|
'validateEdgeTokensSchema',
|
|
|
|
),
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('validateEdgeTokensSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2023-02-15 09:13:32 +01:00
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '/metrics',
|
|
|
|
handler: this.bulkMetrics,
|
|
|
|
permission: NONE, // should have a permission but not bound to any environment
|
|
|
|
middleware: [
|
|
|
|
this.openApiService.validPath({
|
|
|
|
tags: ['Edge'],
|
|
|
|
operationId: 'bulkMetrics',
|
|
|
|
requestBody: createRequestSchema('bulkMetricsSchema'),
|
|
|
|
responses: {
|
|
|
|
202: emptyResponse,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getValidTokens(
|
|
|
|
req: RequestBody<ValidateEdgeTokensSchema>,
|
|
|
|
res: Response<ValidateEdgeTokensSchema>,
|
|
|
|
): Promise<void> {
|
|
|
|
const tokens = await this.edgeService.getValidTokens(
|
|
|
|
req.body.tokens as string[],
|
|
|
|
);
|
|
|
|
this.openApiService.respondWithValidation<ValidateEdgeTokensSchema>(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
validateEdgeTokensSchema.$id,
|
|
|
|
tokens,
|
|
|
|
);
|
|
|
|
}
|
2023-02-15 09:13:32 +01:00
|
|
|
|
|
|
|
async bulkMetrics(
|
|
|
|
req: IAuthRequest<void, void, BulkMetricsSchema>,
|
|
|
|
res: Response<void>,
|
|
|
|
): Promise<void> {
|
|
|
|
const { body, ip: clientIp } = req;
|
|
|
|
const { metrics, applications } = body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let promises: Promise<void>[] = [];
|
|
|
|
for (const app of applications) {
|
|
|
|
promises.push(
|
|
|
|
this.clientInstanceService.registerClient(app, clientIp),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (metrics) {
|
|
|
|
for (const metric of metrics) {
|
|
|
|
promises.push(
|
|
|
|
this.metricsV2.registerClientMetrics(metric, clientIp),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
res.status(202).end();
|
|
|
|
} catch (e) {
|
|
|
|
res.status(400).end();
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|