Add "Add to homarr" feature and move code

This commit is contained in:
Thomas Camlong
2022-07-06 18:08:39 +02:00
parent be770d282a
commit 4b92c52ea8
8 changed files with 232 additions and 159 deletions

View File

@@ -8,7 +8,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const { id } = req.query as { id: string };
const { action } = req.query;
// Get the action on the request (start, stop, restart)
if (action !== 'start' && action !== 'stop' && action !== 'restart') {
if (action !== 'start' && action !== 'stop' && action !== 'restart' && action !== 'remove') {
return res.status(400).json({
statusCode: 400,
message: 'Invalid action',
@@ -29,39 +29,25 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
});
}
});
switch (action) {
case 'start':
container.start((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
case 'stop':
container.stop((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
case 'restart':
container.restart((err, data) => {
if (err) {
res.status(500).json({
message: err,
});
}
});
break;
default:
res.status(400).json({
message: 'Invalid action',
});
try {
switch (action) {
case 'remove':
await container.remove();
break;
case 'start':
container.start();
break;
case 'stop':
container.stop();
break;
case 'restart':
container.restart();
break;
}
} catch (err) {
res.status(500).json({
message: err,
});
}
return res.status(200).json({
success: true,

View File

@@ -5,12 +5,8 @@ import Docker from 'dockerode';
const docker = new Docker();
async function Get(req: NextApiRequest, res: NextApiResponse) {
docker.listContainers({ all: true }, (err, containers) => {
if (err) {
res.status(500).json({ error: err });
}
return res.status(200).json(containers);
});
const containers = await docker.listContainers({ all: true });
return res.status(200).json(containers);
}
export default async (req: NextApiRequest, res: NextApiResponse) => {