mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-22 01:19:18 -05:00
feat(api): returns custom response
This commit is contained in:
parent
3cab0da9a4
commit
61985f7dfe
@ -3,13 +3,13 @@ import type {
|
|||||||
RemoveContainerParams,
|
RemoveContainerParams,
|
||||||
RemoveContainerQuery,
|
RemoveContainerQuery,
|
||||||
} from "#src/types/Container.ts";
|
} 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";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
export default (fastify: FastifyInstance) => {
|
export default (fastify: FastifyInstance) => {
|
||||||
fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => {
|
fastify.get("/", (req: FastifyRequest<{ Params: Container }>) => {
|
||||||
const container = getContainer(req.params.id);
|
const container = getContainer(req.params.id);
|
||||||
return container.inspect();
|
return getContainerResponse(container);
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.delete(
|
fastify.delete(
|
||||||
@ -23,7 +23,7 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const container = getContainer(req.params.id);
|
const container = getContainer(req.params.id);
|
||||||
|
|
||||||
await container.remove({ force: req.query.force === "true" });
|
await container.remove({ force: req.query.force === "true" });
|
||||||
return container;
|
return getContainerResponse(container);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,11 @@ import type {
|
|||||||
RemoveContainerParams,
|
RemoveContainerParams,
|
||||||
RemoveContainerQuery,
|
RemoveContainerQuery,
|
||||||
} from "#src/types/Container.ts";
|
} 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 { getImage } from "#src/utils/images.ts";
|
||||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
@ -33,7 +37,7 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const newContainer = await createContainer({ name, image });
|
const newContainer = await createContainer({ name, image });
|
||||||
await newContainer.start();
|
await newContainer.start();
|
||||||
|
|
||||||
return newContainer.inspect();
|
return getContainerResponse(newContainer);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { Container } from "#src/types/Container.ts";
|
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";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
export default (fastify: FastifyInstance) => {
|
export default (fastify: FastifyInstance) => {
|
||||||
@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const container = getContainer(req.params.id);
|
const container = getContainer(req.params.id);
|
||||||
|
|
||||||
await container.restart();
|
await container.restart();
|
||||||
return container.inspect();
|
return getContainerResponse(container);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { Container } from "#src/types/Container.ts";
|
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";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
export default (fastify: FastifyInstance) => {
|
export default (fastify: FastifyInstance) => {
|
||||||
@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const container = getContainer(req.params.id);
|
const container = getContainer(req.params.id);
|
||||||
|
|
||||||
await container.start();
|
await container.start();
|
||||||
return container.inspect();
|
return getContainerResponse(container);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { Container } from "#src/types/Container.ts";
|
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";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
export default (fastify: FastifyInstance) => {
|
export default (fastify: FastifyInstance) => {
|
||||||
@ -7,6 +7,6 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const container = getContainer(req.params.id);
|
const container = getContainer(req.params.id);
|
||||||
|
|
||||||
await container.stop();
|
await container.stop();
|
||||||
return container.inspect();
|
return getContainerResponse(container);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -1,11 +1,29 @@
|
|||||||
import createContainerSchema from "#src/schemas/createContainerSchema.ts";
|
import createContainerSchema from "#src/schemas/createContainerSchema.ts";
|
||||||
import type { CreateContainerBody } from "#src/types/Container.ts";
|
import type { CreateContainerBody, Container } from "#src/types/Container.ts";
|
||||||
import { createContainer, getContainers } from "#src/utils/containers.ts";
|
import {
|
||||||
|
createContainer,
|
||||||
|
getContainerResponse,
|
||||||
|
getContainers,
|
||||||
|
} 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("/", () => {
|
fastify.get("/", async () => {
|
||||||
return getContainers();
|
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(
|
fastify.post(
|
||||||
@ -15,7 +33,7 @@ export default (fastify: FastifyInstance) => {
|
|||||||
const container = await createContainer({ image: req.body.image });
|
const container = await createContainer({ image: req.body.image });
|
||||||
await container.start();
|
await container.start();
|
||||||
|
|
||||||
return container.inspect();
|
return getContainerResponse(container);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -2,6 +2,8 @@ export interface Container {
|
|||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
image: string;
|
image: string;
|
||||||
|
status: string;
|
||||||
|
ip: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateContainerBody {
|
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";
|
import Dockerode from "dockerode";
|
||||||
|
|
||||||
const dockerode = new Dockerode();
|
const dockerode = new Dockerode();
|
||||||
@ -28,3 +28,17 @@ export const createContainer = ({ name, image }: CreateContainerBody) => {
|
|||||||
|
|
||||||
return container;
|
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