Skip to content

Commit

Permalink
add endpoint availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed Jun 6, 2024
1 parent 8fb827c commit 455903e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/controllers/v3/EndpointsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
Controller,
OperationId,
Get,
Route,
Tags,
Query,
Request,
} from 'tsoa';
import { KeystoneService } from '../ioc/keystoneInjector';
import { inject, injectable } from 'tsyringe';
import { getRecords } from '../../batch/feed-worker';
import { GatewayRoute } from './types';

@injectable()
@Route('/routes')
@Tags('Service Routes')
export class EndpointsController extends Controller {
private keystone: KeystoneService;
constructor(@inject('KeystoneService') private _keystone: KeystoneService) {
super();
this.keystone = _keystone;
}

@Get('availability')
@OperationId('check-availability')
public async check(
@Query() serviceName: string,
@Request() request: any
): Promise<any> {
const ctx = this.keystone.sudo();
const records = await getRecords(
ctx,
'GatewayRoute',
'allGatewayRoutes',
[]
);

let counter = 0;
let matchHostList;
do {
counter++;
matchHostList = this.getMatchHostList(
counter == 1 ? serviceName : `${serviceName}-${counter}`
);
} while (this.isTaken(records, matchHostList));

return {
available: counter == 1 ? 'yes' : 'no',
suggestion: matchHostList[0],
};
}

private getMatchHostList(serviceName: string): string[] {
return [
`${serviceName}.api.gov.bc.ca`,
`${serviceName}-api-gov-bc-ca.dev.api.gov.bc.ca`,
`${serviceName}-api-gov-bc-ca.test.api.gov.bc.ca`,
];
}

private isTaken(records: any[], matchHosts: string[]): boolean {
return (
records.filter(
(r: GatewayRoute) =>
r.hosts.filter((h: string) => matchHosts.indexOf(h) >= 0).length > 0
).length > 0
);
}
}
19 changes: 19 additions & 0 deletions src/controllers/v3/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,25 @@ paths:
required: true
schema:
type: string
/routes/availability:
get:
operationId: check-availability
responses:
'200':
description: Ok
content:
application/json:
schema: {}
tags:
- 'Service Routes'
security: []
parameters:
-
in: query
name: serviceName
required: true
schema:
type: string
/gateways/report:
get:
operationId: report
Expand Down
31 changes: 31 additions & 0 deletions src/controllers/v3/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { DirectoryController } from './DirectoryController';
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { DatasetController } from './DatasetController';
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { EndpointsController } from './EndpointsController';
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { NamespaceController } from './GatewayController';
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { NamespaceDirectoryController } from './GatewayDirectoryController';
Expand Down Expand Up @@ -617,6 +619,35 @@ export function RegisterRoutes(app: express.Router) {
}
});
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
app.get('/ds/api/v3/routes/availability',

async function EndpointsController_check(request: any, response: any, next: any) {
const args = {
serviceName: {"in":"query","name":"serviceName","required":true,"dataType":"string"},
request: {"in":"request","name":"request","required":true,"dataType":"object"},
};

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request, response);

const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(request) : iocContainer;

const controller: any = await container.get<EndpointsController>(EndpointsController);
if (typeof controller['setStatus'] === 'function') {
controller.setStatus(undefined);
}


const promise = controller.check.apply(controller, validatedArgs as any);
promiseHandler(controller, promise, response, undefined, next);
} catch (err) {
return next(err);
}
});
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
app.get('/ds/api/v3/gateways/report',
authenticateMiddleware([{"jwt":[]}]),

Expand Down

0 comments on commit 455903e

Please sign in to comment.