Skip to content

Commit

Permalink
feat: show url routes by default on api
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree committed Feb 22, 2024
1 parent f3a1e79 commit 57534cb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 68 deletions.
1 change: 1 addition & 0 deletions middleware/api/.env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
API_SVC_DOMAIN_NAME = api.sandbox.nekosgate.com
CHARACTER_SHEETS_SVC_DOMAIN_NAME = character-sheets.sandbox.nekosgate.com
EMAIL_SERVICE_SVC_DOMAIN_NAME = email-message.sandbox.nekosgate.com
HTML_TO_PDF_SVC_DOMAIN_NAME = html-to-pdf.sandbox.nekosgate.com
Expand Down
9 changes: 8 additions & 1 deletion middleware/api/openapi-spec.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{
"openapi": "3.0.0",
"paths": {
"/routes": {
"/": {
"get": {
"operationId": "RouterController_getRoutes",
"parameters": [],
"responses": { "200": { "description": "" } }
}
},
"/debug": {
"get": {
"operationId": "RouterController_getConfig",
"parameters": [],
"responses": { "200": { "description": "" } }
}
},
"/*": {
"get": {
"operationId": "RouterController_getRoute",
Expand Down
31 changes: 31 additions & 0 deletions middleware/api/src/modules/router/route-url.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IsString, IsUrl } from '@cats-cradle/validation-schemas';

export class RouteUrlsDto {
@IsString()
@IsUrl()
users_url: string;

@IsString()
@IsUrl()
character_sheets_url: string;

@IsString()
@IsUrl()
email_messages_url: string;

@IsString()
@IsUrl()
html_to_pdf_url: string;

@IsString()
@IsUrl()
instances_url: string;

@IsString()
@IsUrl()
dice_url: string;

@IsString()
@IsUrl()
player_achievements_url: string;
}
10 changes: 8 additions & 2 deletions middleware/api/src/modules/router/router.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import {
VERSION_NEUTRAL,
} from '@nestjs/common';
import { RouterService } from './router.service';
import { RouteUrlsDto } from './route-url.dto';

@Controller({ path: '', version: ['1', VERSION_NEUTRAL] })
export class RouterController {
constructor(private readonly routerService: RouterService) {}

@Get('routes')
getRoutes(): { [key: string]: { path: string; endpoint: string } } {
@Get('')
getRoutes(): RouteUrlsDto {
return this.routerService.getRoutes();
}

@Get('debug')
getConfig(): { [key: string]: { path: string; endpoint: string } } {
return this.routerService.getConfig();
}

@Get('*')
async getRoute(@Req() req: any): Promise<any> {
const { path, query } = req;
Expand Down
62 changes: 13 additions & 49 deletions middleware/api/src/modules/router/router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,11 @@
import { Injectable } from '@nestjs/common';
import axios, { AxiosResponse } from 'axios';
import { v4 as uuidv4 } from 'uuid';

type TRoute = {
path: string;
name: string;
endpoint: string;
};
import { RouteUrlsDto } from './route-url.dto';
import { routes, RouteDefinition } from './routes.config';

@Injectable()
export class RouterService {
private readonly routes: TRoute[] = [
{
name: 'users_url',
path: '/user',
endpoint: process.env.USER_SVC_DOMAIN_NAME || '',
},
{
name: 'character_sheets_url',
path: '/character-sheets',
endpoint: process.env.CHARACTER_SHEETS_SVC_DOMAIN_NAME || '',
},
{
name: 'email_messages_url',
path: '/email-message',
endpoint: process.env.EMAIL_MESSAGE_SVC_DOMAIN_NAME || '',
},
{
name: 'html_to_pdf_url',
path: '/html-to-pdf',
endpoint: process.env.HTML_TO_PDF_SVC_DOMAIN_NAME || '',
},
{
name: 'instances_url',
path: '/instances',
endpoint: process.env.INSTANCES_SVC_DOMAIN_NAME || '',
},
{
name: 'dice_url',
path: '/dice',
endpoint: process.env.LUCK_BY_DICE_SVC_DOMAIN_NAME || '',
},
{
name: 'player_achievements_url',
path: '/player-achievements',
endpoint: process.env.PLAYER_ACHIEVEMENTS_SVC_DOMAIN_NAME || '',
},
// Add more routes as needed
];

async routeRequest(path: string, body: any, method: string): Promise<any> {
const route = this.getRouteForPath(path); // Find the route for the provided path
if (!route) {
Expand Down Expand Up @@ -111,10 +68,17 @@ export class RouterService {
return response.data;
}

// TODO remove endpoint
getRoutes(): { [key: string]: { path: string; endpoint: string } } {
getRoutes(): RouteUrlsDto {
const urls: { [key: string]: string } = {};
routes.forEach((route: RouteDefinition) => {
urls[route.name] = `https://${process.env.API_SVC_DOMAIN_NAME}${route.path}`;
});
return urls as any as RouteUrlsDto;
}

getConfig(): { [key: string]: { path: string; endpoint: string } } {
const urls: { [key: string]: { path: string; endpoint: string } } = {};
this.routes.forEach((route: TRoute) => {
routes.forEach((route: RouteDefinition) => {
urls[route.name] = { path: route.path, endpoint: route.endpoint };
});
return urls;
Expand All @@ -123,7 +87,7 @@ export class RouterService {
private getRouteForPath(
path: string,
): { path: string; endpoint: string } | undefined {
return this.routes.find((route) => path.startsWith(route.path));
return routes.find((route) => path.startsWith(route.path));
}

private constructUrl(endpoint: string, path: string): string {
Expand Down
44 changes: 44 additions & 0 deletions middleware/api/src/modules/router/routes.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export type RouteDefinition = {
path: string;
name: string;
endpoint: string;
};

export const routes: RouteDefinition[] = [
{
name: 'users_url',
path: '/user',
endpoint: process.env.USER_SVC_DOMAIN_NAME || '',
},
{
name: 'character_sheets_url',
path: '/character-sheets',
endpoint: process.env.CHARACTER_SHEETS_SVC_DOMAIN_NAME || '',
},
{
name: 'email_messages_url',
path: '/email-message',
endpoint: process.env.EMAIL_MESSAGE_SVC_DOMAIN_NAME || '',
},
{
name: 'html_to_pdf_url',
path: '/html-to-pdf',
endpoint: process.env.HTML_TO_PDF_SVC_DOMAIN_NAME || '',
},
{
name: 'instances_url',
path: '/instances',
endpoint: process.env.INSTANCES_SVC_DOMAIN_NAME || '',
},
{
name: 'dice_url',
path: '/dice',
endpoint: process.env.LUCK_BY_DICE_SVC_DOMAIN_NAME || '',
},
{
name: 'player_achievements_url',
path: '/player-achievements',
endpoint: process.env.PLAYER_ACHIEVEMENTS_SVC_DOMAIN_NAME || '',
},
// Add more routes as needed
];
3 changes: 2 additions & 1 deletion middleware/api/stacks/__snapshots__/main.stack.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,11 @@ exports[`MainStack should match snapshot test 1`] = `
S3Bucket: {
Fn::Sub: cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region},
},
S3Key: 84733bfcc0860a158020056aeea6de14f9009718cf2a83659cde3585250b04a6.zip,
S3Key: 696402484aadd93c03efb231f3a270bfd8656a85eed0051095cb57022377084a.zip,
},
Environment: {
Variables: {
API_SVC_DOMAIN_NAME: {{resolve:ssm:/API_SVC_DOMAIN_NAME}},
AWS_ACCOUNT_ID: {
Ref: AWS::AccountId,
},
Expand Down
27 changes: 12 additions & 15 deletions middleware/api/stacks/main.stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Secret {

export class MainStack extends cdk.Stack {
secrets: Secret[] = [
{ key: 'API_SVC_DOMAIN_NAME' },
{ key: 'USER_SVC_DOMAIN_NAME' },
{ key: 'CHARACTER_SHEETS_SVC_DOMAIN_NAME' },
{ key: 'EMAIL_SERVICE_SVC_DOMAIN_NAME' },
Expand All @@ -23,20 +24,6 @@ export class MainStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const healthCheckLambda = this.setupLambda(`${id}-health-check-lambda`);

const lambdaDomainName = new LambdaDomainName(this, `${id}-dns-domain`, {
subdomainName: 'api',
proxyLambda: healthCheckLambda,
stageName: 'default',
});

new cdk.CfnOutput(this, 'Endpoint', {
value: lambdaDomainName.getBaseUrl(),
});
}

setupLambda(lambdaId: string): lambda.Function {
const environment: { [key: string]: string } = {};

for (const secret of this.secrets) {
Expand All @@ -49,6 +36,16 @@ export class MainStack extends cdk.Stack {
environment: environment,
});

return lambdaFunction.getNodeJsFunction();
const lambdaDomainName = new LambdaDomainName(this, `${id}-dns-domain`, {
subdomainName: 'api',
proxyLambda: lambdaFunction.getNodeJsFunction(),
stageName: 'default',
});

lambdaDomainName.domainName;

new cdk.CfnOutput(this, 'Endpoint', {
value: lambdaDomainName.getBaseUrl(),
});
}
}

0 comments on commit 57534cb

Please sign in to comment.