From 61985f7dfe862a8e87544e7e318747ad6b250019 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Tue, 14 Jan 2025 08:17:56 -0500 Subject: [PATCH] feat(api): returns custom response --- src/routes/containers/_id/index.ts | 6 +++--- src/routes/containers/_id/reinstall.ts | 8 ++++++-- src/routes/containers/_id/restart.ts | 4 ++-- src/routes/containers/_id/start.ts | 4 ++-- src/routes/containers/_id/stop.ts | 4 ++-- src/routes/containers/index.ts | 28 +++++++++++++++++++++----- src/types/Container.ts | 2 ++ src/utils/containers.ts | 16 ++++++++++++++- 8 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/routes/containers/_id/index.ts b/src/routes/containers/_id/index.ts index 92c2421..68525ed 100644 --- a/src/routes/containers/_id/index.ts +++ b/src/routes/containers/_id/index.ts @@ -3,13 +3,13 @@ import type { RemoveContainerParams, RemoveContainerQuery, } from "#src/types/Container.ts"; -import { getContainer } from "#src/utils/containers.ts"; +import { getContainer, getContainerResponse } 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(); + return getContainerResponse(container); }); fastify.delete( @@ -23,7 +23,7 @@ export default (fastify: FastifyInstance) => { const container = getContainer(req.params.id); await container.remove({ force: req.query.force === "true" }); - return container; + return getContainerResponse(container); }, ); }; diff --git a/src/routes/containers/_id/reinstall.ts b/src/routes/containers/_id/reinstall.ts index c93beef..0f5436d 100644 --- a/src/routes/containers/_id/reinstall.ts +++ b/src/routes/containers/_id/reinstall.ts @@ -3,7 +3,11 @@ import type { RemoveContainerParams, RemoveContainerQuery, } from "#src/types/Container.ts"; -import { createContainer, getContainer } from "#src/utils/containers.ts"; +import { + createContainer, + getContainer, + getContainerResponse, +} from "#src/utils/containers.ts"; import { getImage } from "#src/utils/images.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; @@ -33,7 +37,7 @@ export default (fastify: FastifyInstance) => { const newContainer = await createContainer({ name, image }); await newContainer.start(); - return newContainer.inspect(); + return getContainerResponse(newContainer); }, ); }; diff --git a/src/routes/containers/_id/restart.ts b/src/routes/containers/_id/restart.ts index ef63cdc..fcb094d 100644 --- a/src/routes/containers/_id/restart.ts +++ b/src/routes/containers/_id/restart.ts @@ -1,5 +1,5 @@ import type { Container } from "#src/types/Container.ts"; -import { getContainer } from "#src/utils/containers.ts"; +import { getContainer, getContainerResponse } from "#src/utils/containers.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; export default (fastify: FastifyInstance) => { @@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => { const container = getContainer(req.params.id); await container.restart(); - return container.inspect(); + return getContainerResponse(container); }); }; diff --git a/src/routes/containers/_id/start.ts b/src/routes/containers/_id/start.ts index ceddc42..7b3bba4 100644 --- a/src/routes/containers/_id/start.ts +++ b/src/routes/containers/_id/start.ts @@ -1,5 +1,5 @@ import type { Container } from "#src/types/Container.ts"; -import { getContainer } from "#src/utils/containers.ts"; +import { getContainer, getContainerResponse } from "#src/utils/containers.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; export default (fastify: FastifyInstance) => { @@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => { const container = getContainer(req.params.id); await container.start(); - return container.inspect(); + return getContainerResponse(container); }); }; diff --git a/src/routes/containers/_id/stop.ts b/src/routes/containers/_id/stop.ts index 668c9fe..8fe20b7 100644 --- a/src/routes/containers/_id/stop.ts +++ b/src/routes/containers/_id/stop.ts @@ -1,5 +1,5 @@ import type { Container } from "#src/types/Container.ts"; -import { getContainer } from "#src/utils/containers.ts"; +import { getContainer, getContainerResponse } from "#src/utils/containers.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; export default (fastify: FastifyInstance) => { @@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => { const container = getContainer(req.params.id); await container.stop(); - return container.inspect(); + return getContainerResponse(container); }); }; diff --git a/src/routes/containers/index.ts b/src/routes/containers/index.ts index 69376bf..0531bc3 100644 --- a/src/routes/containers/index.ts +++ b/src/routes/containers/index.ts @@ -1,11 +1,29 @@ import createContainerSchema from "#src/schemas/createContainerSchema.ts"; -import type { CreateContainerBody } from "#src/types/Container.ts"; -import { createContainer, getContainers } from "#src/utils/containers.ts"; +import type { CreateContainerBody, Container } from "#src/types/Container.ts"; +import { + createContainer, + getContainerResponse, + getContainers, +} from "#src/utils/containers.ts"; import type { FastifyInstance, FastifyRequest } from "fastify"; export default (fastify: FastifyInstance) => { - fastify.get("/", () => { - return getContainers(); + fastify.get("/", async () => { + const containers = await getContainers(); + const containersResponse = []; + + for (const container of containers) { + const response = { + id: container.Id, + name: container.Names[0].slice(1), + image: container.Image, + status: container.State, + } as Omit; + + containersResponse.push(response); + } + + return containersResponse; }); fastify.post( @@ -15,7 +33,7 @@ export default (fastify: FastifyInstance) => { const container = await createContainer({ image: req.body.image }); await container.start(); - return container.inspect(); + return getContainerResponse(container); }, ); }; diff --git a/src/types/Container.ts b/src/types/Container.ts index 723c80b..8970526 100644 --- a/src/types/Container.ts +++ b/src/types/Container.ts @@ -2,6 +2,8 @@ export interface Container { id: string; name: string; image: string; + status: string; + ip: string; } export interface CreateContainerBody { diff --git a/src/utils/containers.ts b/src/utils/containers.ts index c589b1f..283faee 100644 --- a/src/utils/containers.ts +++ b/src/utils/containers.ts @@ -1,4 +1,4 @@ -import type { CreateContainerBody } from "#src/types/Container.ts"; +import type { Container, CreateContainerBody } from "#src/types/Container.ts"; import Dockerode from "dockerode"; const dockerode = new Dockerode(); @@ -28,3 +28,17 @@ export const createContainer = ({ name, image }: CreateContainerBody) => { return container; }; + +export const getContainerResponse = async (container: Dockerode.Container) => { + const inspect = await container.inspect(); + + const response = { + id: inspect.Id, + name: inspect.Name.slice(1), + image: inspect.Config.Image, + status: inspect.State.Status, + ip: inspect.NetworkSettings.Networks.bridge.IPAddress, + } as Container; + + return response; +};