Skip to content

Commit

Permalink
improve resources routes
Browse files Browse the repository at this point in the history
add /names
improve /restart, /start & /stop
  • Loading branch information
Z3rio committed Sep 16, 2023
1 parent 8354013 commit 2195578
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions src/server/modules/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,56 @@ export function createResources(app: Express) {
});
});

app.get("/resources/names", (req: Request, res: Response) => {
res.status(200);
const resources = [];

if (req.query && req.query.states) {
const states = req.query.states.toString().split(",");

for (let i = 1; i < GetNumResources(); i++) {
const resourceName = GetResourceByFindIndex(i);

if (states.includes(GetResourceState(resourceName))) {
resources.push(resourceName);
}
}
} else {
for (let i = 1; i < GetNumResources(); i++) {
resources.push(GetResourceByFindIndex(i));
}
}

res.json({
list: resources,
});
});

app.post("/resources/:name/restart", (req: Request, res: Response) => {
if (req.params && req.params.name) {
ExecuteCommand(`restart ${req.params.name}`);
res.status(200);
res.json({});
if (GetResourceState(req.params.name) !== "started") {
res.json({
err: "Resource is not started",
});
} else {
ExecuteCommand(`restart ${req.params.name}`);
res.json({});
}
}
});

app.post("/resources/:name/stop", (req: Request, res: Response) => {
if (req.params && req.params.name) {
ExecuteCommand(`stop ${req.params.name}`);
res.status(200);
res.json({});
if (GetResourceState(req.params.name) !== "started") {
res.json({
err: "Resource is not started",
});
} else {
ExecuteCommand(`stop ${req.params.name}`);
res.json({});
}
}
});

Expand All @@ -47,9 +84,15 @@ export function createResources(app: Express) {

app.post("/resources/:name/start", (req: Request, res: Response) => {
if (req.params && req.params.name) {
ExecuteCommand(`start ${req.params.name}`);
res.status(200);
res.json({});
if (GetResourceState(req.params.name) !== "stopped") {
res.json({
err: "Resource is not stopped",
});
} else {
ExecuteCommand(`start ${req.params.name}`);
res.json({});
}
}
});
}

0 comments on commit 2195578

Please sign in to comment.