-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fb827c
commit 455903e
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters