mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-13 15:51:00 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import { AdminGuard } from '../auth-core/admin.guard';
|
|
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
|
|
import { RouteUsageReportDto } from './route-usage.dto';
|
|
import { RouteUsageService } from './route-usage.service';
|
|
import { Body, Controller, Delete, Get, HttpCode, Post, UseGuards } from '@nestjs/common';
|
|
import type { RouteUsageReportResult, RouteUsageSummaryResult } from '@trek/shared';
|
|
|
|
/**
|
|
* /api/route-usage — how much routing this instance does.
|
|
*
|
|
* Reporting is open to any signed-in user, because every signed-in user is the one
|
|
* generating the load; reading is admin only, because the totals are everybody's
|
|
* routing and the person entitled to see that is the person running the instance.
|
|
* Nothing here leaves the instance on its own.
|
|
*/
|
|
@Controller('api/route-usage')
|
|
@UseGuards(JwtAuthGuard)
|
|
export class RouteUsageController {
|
|
constructor(private readonly usage: RouteUsageService) {}
|
|
|
|
/**
|
|
* 200 with `{ recorded: false }` when counting is off, rather than a 403. The
|
|
* client flushes and moves on; a rejection would be a permanent error in the
|
|
* console of every install that switched the counters off.
|
|
*/
|
|
@Post('report')
|
|
@HttpCode(200)
|
|
async report(@Body() body: RouteUsageReportDto): Promise<RouteUsageReportResult> {
|
|
return { recorded: await this.usage.record(body) };
|
|
}
|
|
|
|
@Get('summary')
|
|
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
async summary(): Promise<RouteUsageSummaryResult> {
|
|
return await this.usage.summary();
|
|
}
|
|
|
|
@Delete()
|
|
@UseGuards(JwtAuthGuard, AdminGuard)
|
|
async clear(): Promise<{ removed: number }> {
|
|
return { removed: await this.usage.clear() };
|
|
}
|
|
}
|