apps/api: add admin methods

This commit is contained in:
vchikalkin 2024-03-04 11:59:57 +03:00
parent 47d47a0bfd
commit 6ef2c7ea0f

View File

@ -4,9 +4,12 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager';
import {
All,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Inject,
Query,
Req,
Res,
} from '@nestjs/common';
@ -23,6 +26,7 @@ export class ProxyController {
constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: RedisStore,
) {}
@All('/graphql')
public async graphql(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
const { operationName, query, variables } = req.body as GQLRequest;
@ -55,4 +59,49 @@ export class ProxyController {
return reply.send(data);
}
@Get('/queries')
public async getQueriesList(
@Req() req: FastifyRequest,
@Res() reply: FastifyReply,
) {
const list = await this.cacheManager.store.keys('*');
const res = (Object.keys(queryTTL) as Array<keyof typeof queryTTL>).reduce(
(acc, queryName) => {
const queries = list.filter((x) => x.split(' ').at(0) === queryName);
const ttl = queryTTL[queryName];
acc[queryName] = { queries, ttl };
return acc;
},
{} as Record<string, unknown>,
);
return reply.send(res);
}
@Delete('delete-query')
public async deleteQuery(
@Query('queryName') queryName: string,
@Res() reply: FastifyReply,
) {
try {
await this.cacheManager.del(queryName);
return reply.send('ok');
} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Delete('flush-all')
public async flushAll(@Res() reply: FastifyReply) {
try {
await this.cacheManager.store.reset();
return reply.send('ok');
} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}