@sourceloop/ctrl-plane-orchestrator-service
Version:
ARC SaaS Orchestrator service.
48 lines (44 loc) • 1.33 kB
text/typescript
import {post, param, requestBody, HttpErrors} from '@loopback/rest';
import {inject} from '@loopback/core';
import {
DefaultEventTypes,
OrchestratorServiceBindings,
OrchestratorServiceInterface,
} from '../services/types';
import {AnyObject} from '@loopback/repository';
import {LoggingBindings, WinstonLogger} from '@loopback/logging';
export class EventController {
(LoggingBindings.WINSTON_LOGGER)
private logger: WinstonLogger;
constructor(
(OrchestratorServiceBindings.ORCHESTRATOR_SERVICE)
protected orchestratorService: OrchestratorServiceInterface,
) {}
('/events/{eventType}')
async handleEvent(
.path.string('eventType') eventType: DefaultEventTypes,
({
content: {
'application/json': {
schema: {
type: 'object',
additionalProperties: true,
},
},
},
})
body: AnyObject,
): Promise<object> {
try {
await this.orchestratorService.handleEvent(eventType, body);
return {success: true};
} catch (err) {
if (err instanceof Error) {
throw HttpErrors.BadRequest(err.message);
}
// Log unexpected errors
this.logger.error(err);
throw new HttpErrors.InternalServerError('An unexpected error occurred.');
}
}
}