diff --git a/src/routes/containers/_id/index.ts b/src/routes/containers/_id/index.ts new file mode 100644 index 0000000..92c2421 --- /dev/null +++ b/src/routes/containers/_id/index.ts @@ -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; + }, + ); +}; diff --git a/src/routes/containers/_id/restart.ts b/src/routes/containers/_id/restart.ts new file mode 100644 index 0000000..ef63cdc --- /dev/null +++ b/src/routes/containers/_id/restart.ts @@ -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(); + }); +}; diff --git a/src/routes/containers/_id/start.ts b/src/routes/containers/_id/start.ts new file mode 100644 index 0000000..ceddc42 --- /dev/null +++ b/src/routes/containers/_id/start.ts @@ -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(); + }); +}; diff --git a/src/routes/containers/_id.ts b/src/routes/containers/_id/stop.ts similarity index 75% rename from src/routes/containers/_id.ts rename to src/routes/containers/_id/stop.ts index ec1e10e..668c9fe 100644 --- a/src/routes/containers/_id.ts +++ b/src/routes/containers/_id/stop.ts @@ -3,8 +3,10 @@ import { getContainer } from "#src/utils/containers.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; export default (fastify: FastifyInstance) => { - fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => { + fastify.put("/", async (req: FastifyRequest<{ Params: Container }>) => { const container = getContainer(req.params.id); + + await container.stop(); return container.inspect(); }); }; diff --git a/src/types/Container.ts b/src/types/Container.ts index db6273d..1a2a36c 100644 --- a/src/types/Container.ts +++ b/src/types/Container.ts @@ -7,3 +7,11 @@ export interface Container { export interface CreateContainerBody { image: string; } + +export interface RemoveContainerParams { + id: string; +} + +export interface RemoveContainerQuery { + force: string; +}