feat: container state management

This commit is contained in:
CyberL1 2025-01-07 12:15:23 +01:00
parent 77ce186675
commit e2e4b75681
5 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,29 @@
import type {
Container,
RemoveContainerParams,
RemoveContainerQuery,
} from "#src/types/Container.ts";
import { getContainer } from "#src/utils/containers.ts";
import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => {
const container = getContainer(req.params.id);
return container.inspect();
});
fastify.delete(
"/",
async (
req: FastifyRequest<{
Params: RemoveContainerParams;
Querystring: RemoveContainerQuery;
}>,
) => {
const container = getContainer(req.params.id);
await container.remove({ force: req.query.force === "true" });
return container;
},
);
};

View File

@ -0,0 +1,12 @@
import type { Container } from "#src/types/Container.ts";
import { getContainer } from "#src/utils/containers.ts";
import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.put("/", async (req: FastifyRequest<{ Params: Container }>) => {
const container = getContainer(req.params.id);
await container.restart();
return container.inspect();
});
};

View File

@ -0,0 +1,12 @@
import type { Container } from "#src/types/Container.ts";
import { getContainer } from "#src/utils/containers.ts";
import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.put("/", async (req: FastifyRequest<{ Params: Container }>) => {
const container = getContainer(req.params.id);
await container.start();
return container.inspect();
});
};

View File

@ -3,8 +3,10 @@ import { getContainer } from "#src/utils/containers.ts";
import type { FastifyInstance, FastifyRequest } from "fastify"; import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => { export default (fastify: FastifyInstance) => {
fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => { fastify.put("/", async (req: FastifyRequest<{ Params: Container }>) => {
const container = getContainer(req.params.id); const container = getContainer(req.params.id);
await container.stop();
return container.inspect(); return container.inspect();
}); });
}; };

View File

@ -7,3 +7,11 @@ export interface Container {
export interface CreateContainerBody { export interface CreateContainerBody {
image: string; image: string;
} }
export interface RemoveContainerParams {
id: string;
}
export interface RemoveContainerQuery {
force: string;
}