-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split session and proxy functionality for better API
- Loading branch information
Showing
6 changed files
with
59 additions
and
34 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
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,42 @@ | ||
import { All, Controller, Param, Req, Res } from '@nestjs/common'; | ||
import { ApiTags, ApiOperation, ApiParam } from '@nestjs/swagger'; | ||
import { createRequestObject } from 'src/utils/data'; | ||
import { captureResponseBody } from 'src/utils/responses'; | ||
import { RequestService } from '../requests/request.service'; | ||
import { SessionService } from '../sessions/session.service'; | ||
import { Request, Response } from 'express'; | ||
|
||
@ApiTags('app') | ||
@Controller('app') | ||
export class ProxyAppController { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
private sessionListeners = new Map<string, Function>(); | ||
|
||
constructor( | ||
private readonly sessionService: SessionService, | ||
private readonly requestService: RequestService, | ||
) {} | ||
|
||
@All(':id/**') | ||
@ApiOperation({ summary: 'Proxy request to the target URL' }) | ||
@ApiParam({ name: 'id', description: 'Session ID' }) | ||
async proxyRequestGet(@Param('id') id: string, @Req() req: Request, @Res() res: Response) { | ||
const session = await this.sessionService.findOne(id); | ||
const proxyMiddleware = await this.sessionService.getProxyMiddleware(id); | ||
const responsePromise = captureResponseBody(res); | ||
|
||
// TODO: Implement the logic to manage listener | ||
if (!this.sessionListeners.has(id)) { | ||
const listener = async (req: Request, res: Response) => { | ||
proxyMiddleware(req, res); | ||
const responseBody = await responsePromise; | ||
const target = await this.sessionService.getTarget(id); | ||
await this.requestService.create(createRequestObject(id, target, session, req, res, responseBody)); | ||
}; | ||
this.sessionListeners.set(id, listener); | ||
} | ||
|
||
const listener = this.sessionListeners.get(id); | ||
listener(req, res); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ProxyAppController } from './proxyapp.controller'; | ||
import { SessionModule } from '../sessions/session.module'; | ||
import { RequestModule } from '../requests/request.module'; | ||
|
||
@Module({ | ||
imports: [SessionModule, RequestModule], | ||
controllers: [ProxyAppController], | ||
}) | ||
export class ProxyAppModule {} |
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
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