mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
686592437a
A hung TREK Valhalla cost every leg, height chunk and trace the full 30 s before FOSSGIS answered. The relay now waits 10 s for a route or heights, rests a minute after an error or timeout and answers 503 at once meanwhile, follows a 429's Retry-After (an hour at most), passes that header on to the browser, and holds the installation to 100 questions a minute below the gate's 120 (60 per user).
53 lines
2.3 KiB
TypeScript
53 lines
2.3 KiB
TypeScript
import type { User } from '../../types';
|
|
import { CurrentUser } from '../auth-core/current-user.decorator';
|
|
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
|
|
import { ValhallaHeightDto, ValhallaRouteDto, ValhallaTraceDto } from './routing-relay.dto';
|
|
import { RoutingRelayService, type ValhallaRelayAnswer } from './routing-relay.service';
|
|
import { Body, Controller, HttpCode, Post, Res, UseGuards } from '@nestjs/common';
|
|
|
|
import type { Response } from 'express';
|
|
|
|
/**
|
|
* /api/routing/valhalla/:action: the planner's Valhalla questions, asked of
|
|
* the TREK services through this server. Same origin, so the browser needs no
|
|
* new connect-src, and Valhalla's own answer comes back as it was sent, its
|
|
* refusals included: a 400 that says no way joins the points is the planner's
|
|
* to read.
|
|
*/
|
|
@Controller('api/routing/valhalla')
|
|
@UseGuards(JwtAuthGuard)
|
|
export class RoutingRelayController {
|
|
constructor(private readonly relay: RoutingRelayService) {}
|
|
|
|
// response-contract-exempt: passes Valhalla's own answer through @Res(), status and body as they came.
|
|
@Post('route')
|
|
@HttpCode(200)
|
|
async route(@CurrentUser() user: User, @Body() body: ValhallaRouteDto, @Res() res: Response): Promise<void> {
|
|
passOn(res, await this.relay.ask(user.id, 'route', body));
|
|
}
|
|
|
|
// response-contract-exempt: passes Valhalla's own answer through @Res(), status and body as they came.
|
|
@Post('trace_attributes')
|
|
@HttpCode(200)
|
|
async traceAttributes(
|
|
@CurrentUser() user: User,
|
|
@Body() body: ValhallaTraceDto,
|
|
@Res() res: Response,
|
|
): Promise<void> {
|
|
passOn(res, await this.relay.ask(user.id, 'trace_attributes', body));
|
|
}
|
|
|
|
// response-contract-exempt: passes Valhalla's own answer through @Res(), status and body as they came.
|
|
@Post('height')
|
|
@HttpCode(200)
|
|
async height(@CurrentUser() user: User, @Body() body: ValhallaHeightDto, @Res() res: Response): Promise<void> {
|
|
passOn(res, await this.relay.ask(user.id, 'height', body));
|
|
}
|
|
}
|
|
|
|
/** Of the upstream's headers only Retry-After goes on, so the planner knows how long to ask FOSSGIS instead. */
|
|
function passOn(res: Response, answer: ValhallaRelayAnswer): void {
|
|
if (answer.retryAfter) res.set('Retry-After', answer.retryAfter);
|
|
res.status(answer.status).type(answer.contentType).send(answer.body);
|
|
}
|