adds deleting
This commit is contained in:
@@ -8,6 +8,7 @@ import { ensureDatabaseReady } from "./db-init.js";
|
||||
import { registerRoutes } from "./routes.js";
|
||||
|
||||
const app = Fastify({
|
||||
disableRequestLogging: true,
|
||||
logger: {
|
||||
transport: {
|
||||
target: "pino-pretty",
|
||||
@@ -18,7 +19,11 @@ const app = Fastify({
|
||||
|
||||
await ensureDatabaseReady(app.log);
|
||||
|
||||
await app.register(cors, { origin: true, credentials: true });
|
||||
await app.register(cors, {
|
||||
origin: true,
|
||||
credentials: true,
|
||||
methods: ["GET", "HEAD", "POST", "DELETE", "OPTIONS"],
|
||||
});
|
||||
|
||||
await app.register(swagger, {
|
||||
openapi: {
|
||||
@@ -32,9 +37,39 @@ await app.register(swagger, {
|
||||
await app.register(swaggerUI, { routePrefix: "/docs" });
|
||||
await app.register(sensible);
|
||||
|
||||
app.setErrorHandler((err, _req, reply) => {
|
||||
app.addHook("onRequest", async (req) => {
|
||||
if (req.url.startsWith("/health")) return;
|
||||
req.log.info({ method: req.method, url: req.url }, "request received");
|
||||
});
|
||||
|
||||
app.addHook("onResponse", async (req, reply) => {
|
||||
if (req.url.startsWith("/health")) return;
|
||||
const responseTimeMs = typeof reply.elapsedTime === "number" ? Number(reply.elapsedTime.toFixed(1)) : undefined;
|
||||
req.log.info(
|
||||
{
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
statusCode: reply.statusCode,
|
||||
responseTimeMs,
|
||||
},
|
||||
"request completed"
|
||||
);
|
||||
});
|
||||
|
||||
app.setErrorHandler((err, req, reply) => {
|
||||
const e = err as any;
|
||||
const statusCode = e.statusCode ?? 500;
|
||||
if (!req.url.startsWith("/health")) {
|
||||
req.log.error(
|
||||
{
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
statusCode,
|
||||
err: e,
|
||||
},
|
||||
"request failed"
|
||||
);
|
||||
}
|
||||
reply.status(statusCode).send({
|
||||
error: true,
|
||||
message: e.message ?? String(e),
|
||||
|
||||
@@ -50,7 +50,7 @@ async function storeNonAssistantMessages(chatId: string, messages: IncomingChatM
|
||||
}
|
||||
|
||||
export async function registerRoutes(app: FastifyInstance) {
|
||||
app.get("/health", async () => ({ ok: true }));
|
||||
app.get("/health", { logLevel: "silent" }, async () => ({ ok: true }));
|
||||
|
||||
app.get("/v1/auth/session", async (req) => {
|
||||
requireAdmin(req);
|
||||
@@ -75,6 +75,23 @@ export async function registerRoutes(app: FastifyInstance) {
|
||||
return { chat };
|
||||
});
|
||||
|
||||
app.delete("/v1/chats/:chatId", async (req) => {
|
||||
requireAdmin(req);
|
||||
const Params = z.object({ chatId: z.string() });
|
||||
const { chatId } = Params.parse(req.params);
|
||||
|
||||
req.log.info({ chatId }, "delete chat requested");
|
||||
|
||||
const result = await prisma.chat.deleteMany({ where: { id: chatId } });
|
||||
if (result.count === 0) {
|
||||
req.log.warn({ chatId }, "delete chat target not found");
|
||||
return app.httpErrors.notFound("chat not found");
|
||||
}
|
||||
|
||||
req.log.info({ chatId }, "chat deleted");
|
||||
return { deleted: true };
|
||||
});
|
||||
|
||||
app.get("/v1/searches", async (req) => {
|
||||
requireAdmin(req);
|
||||
const searches = await prisma.search.findMany({
|
||||
@@ -101,6 +118,22 @@ export async function registerRoutes(app: FastifyInstance) {
|
||||
return { search };
|
||||
});
|
||||
|
||||
app.delete("/v1/searches/:searchId", async (req) => {
|
||||
requireAdmin(req);
|
||||
const Params = z.object({ searchId: z.string() });
|
||||
const { searchId } = Params.parse(req.params);
|
||||
|
||||
req.log.info({ searchId }, "delete search requested");
|
||||
|
||||
const result = await prisma.search.deleteMany({ where: { id: searchId } });
|
||||
if (result.count === 0) {
|
||||
req.log.warn({ searchId }, "delete search target not found");
|
||||
return app.httpErrors.notFound("search not found");
|
||||
}
|
||||
req.log.info({ searchId }, "search deleted");
|
||||
return { deleted: true };
|
||||
});
|
||||
|
||||
app.get("/v1/searches/:searchId", async (req) => {
|
||||
requireAdmin(req);
|
||||
const Params = z.object({ searchId: z.string() });
|
||||
|
||||
Reference in New Issue
Block a user