fix(server): container stats sending

This commit is contained in:
CyberL1 2025-01-14 17:08:12 -05:00
parent 63ae224084
commit f4161b4f47

View File

@ -3,8 +3,27 @@ import { getContainer } from "#src/utils/containers.ts";
import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => {
fastify.get(
"/",
async (req: FastifyRequest<{ Params: Container }>, reply) => {
const container = getContainer(req.params.id);
return container.stats();
reply.header("Content-Type", "text/event-stream");
reply.header("Cache-Control", "no-cache");
reply.header("Connection", "keep-alive");
const stream = await container.stats({ stream: true });
if (!stream) {
reply.status(500).send("Error fetching stats");
return;
}
stream.on("data", (chunk) => {
reply.raw.write(`data: ${chunk.toString()}\n\n`);
});
stream.on("end", () => reply.raw.end());
return stream;
},
);
};