mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-21 17:09:19 -05:00
feat(api): returns custom response
This commit is contained in:
parent
3cab0da9a4
commit
61985f7dfe
@ -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);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
@ -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);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
@ -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<Container, "ip">;
|
||||
|
||||
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);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
@ -2,6 +2,8 @@ export interface Container {
|
||||
id: string;
|
||||
name: string;
|
||||
image: string;
|
||||
status: string;
|
||||
ip: string;
|
||||
}
|
||||
|
||||
export interface CreateContainerBody {
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user