feat: add local machine registry foundation

This commit is contained in:
Marc Kassubeck
2026-05-26 13:04:00 +02:00
parent 712456f953
commit 0405b384b1
18 changed files with 673 additions and 28 deletions
+44
View File
@@ -0,0 +1,44 @@
import type { FastifyInstance } from "fastify";
import { MachineService, type CreateMachineInput, type UpdateMachineInput } from "./machineService.js";
export function registerMachineRoutes(app: FastifyInstance, machines = new MachineService()): void {
app.get("/api/machines", async () => ({ machines: await machines.list() }));
app.post<{ Body: CreateMachineInput }>("/api/machines", async (request, reply) => {
try {
return await machines.add(request.body);
} catch (error) {
return reply.code(400).send({ error: errorMessage(error) });
}
});
app.get<{ Params: { machineId: string } }>("/api/machines/:machineId", async (request, reply) => {
const machine = await machines.get(request.params.machineId);
if (machine === undefined) return reply.code(404).send({ error: "Machine not found" });
return machine;
});
app.patch<{ Params: { machineId: string }; Body: UpdateMachineInput }>("/api/machines/:machineId", async (request, reply) => {
try {
const machine = await machines.update(request.params.machineId, request.body);
if (machine === undefined) return await reply.code(404).send({ error: "Machine not found" });
return machine;
} catch (error) {
return reply.code(400).send({ error: errorMessage(error) });
}
});
app.delete<{ Params: { machineId: string } }>("/api/machines/:machineId", async (request, reply) => {
try {
const removed = await machines.remove(request.params.machineId);
if (!removed) return await reply.code(404).send({ error: "Machine not found" });
return { deleted: true };
} catch (error) {
return reply.code(400).send({ error: errorMessage(error) });
}
});
}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}