Skip to content

Commit

Permalink
ContainerEndpoints (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
taos15 authored Sep 10, 2023
1 parent 514b704 commit bdc6df1
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 23 deletions.
1 change: 0 additions & 1 deletion .github/workflows/Master CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
uses: docker/build-push-action@v4
with:
platforms: linux/amd64
# platforms: linux/amd64,linux/arm64,linux/arm/v7
context: .
file: ./Dockerfile
push: true
Expand Down
121 changes: 121 additions & 0 deletions Endpoints.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Documentation

## Traefik-http-api

### Server

#### GET Servers

```http
localhost/api/v1/servers
```

#### GET Server By Name

```http
localhost/api/v1/servers/:name
```

#### POST Create Servers

```http
localhost/api/v1/servers
```

##### Body

```json
{
"name": "test",
"host": "<http://192.168.1.32>",
"port": 2375,
"enable": true
}
```

#### PUT Edit Server

```http
localhost/api/v1/servers/:id
```

##### Body

```json
{
"name": "test4",
"host": "<http://192.168.1.32>",
"port": 2375,
"enable": true
}
```

#### DELETE Delete Server

```http
localhost/api/v1/servers/:id
```

##### Body

```json
{
"name": "test4",
"host": "<http://192.168.1.32>",
"port": 2375,
"enable": true
}
```

### Docker Container

#### GET Containers All

```http
localhost/api/v1/containers
```

#### GET Containers By Server

```http
localhost/api/v1/containers/server/:name
```

#### GET Container By Id

```http
localhost/api/v1/containers/:id
```

##### Body

```json
{
"name": "Servername"
}
```

#### POST Container Command

The following commands are available: start, stop, pause, unpause, and restart.

```http
localhost/api/v1/containers/:id
```

##### Body

```json
{
"name": "ServerName",
"command": "start"
}
```

### Traefik

#### GET Traefik Config

```http
localhost/api/v1/traefikconfig
```
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@ pnpm run start
- If you get an error saying that the port is been use you can run `lsof -ti :4001 | xargs kill -9` to kill the
process running in that port.

## API

### /api/:ver/servers/

GET/Post of the servers in the database

### /api/:ver/servers/:id

PUT modify Specify server in the database

### /api/:ver/servers/:id

DEL Delete Specify server from the database

### /api/:ver/traefikconfig

GET Traefik dynamic config in json format

## Treafik setup

you need to add a `http` provider to your traefik static config. It looks like this:
Expand Down
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cors from "cors";
import express, { Request, Response } from "express";
import morgan from "morgan";
import { authRouter } from "./routes/authRoute";
import { dockerContainerRoute } from "./routes/dockerContainerRoute";
import { serverRouter } from "./routes/serverRoute";
import { traefikRouter } from "./routes/traefikRoute";

Expand All @@ -12,6 +13,7 @@ app.use(express.json());
app.use(morgan("dev"));
app.use("/auth", authRouter);
app.use("/api/v1/servers", serverRouter);
app.use("/api/v1/containers", dockerContainerRoute);
app.use("/api/v1/traefikconfig", traefikRouter);

export const domain = process.env.DOMAIN;
Expand Down
128 changes: 128 additions & 0 deletions src/controllers/dockerContainerControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import Docker from "dockerode";
import { Request, Response } from "express";
import { prisma } from "../db/client";
import { findDockerInstance } from "../utils/docker/findDockerInstance";
import { gertContainersList } from "../utils/docker/gertContainersList";
import { getCombinedContainerlist } from "../utils/docker/getCombinedContainerlist";

export const dockerContainer_getAll = (req: Request, res: Response) => {
(async () => {
try {
const allContainers = await getCombinedContainerlist();
res.status(200).json(allContainers);
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};

export const dockerContainer_getByServer = (req: Request, res: Response) => {
(async () => {
try {
const { name } = req.params;
console.log(name);
const dockerServer = await findDockerInstance(name);

const containers = await dockerServer.listContainers({ all: true });
res.status(200).json(containers);
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};
export const dockerContainer_getByID = (req: Request, res: Response) => {
(async () => {
try {
const { id } = req.params;
const { name } = req.body;
console.log(name);
const dockerServer = await findDockerInstance(name);

const container = dockerServer.getContainer(id);

res.status(200).json(await container.inspect());
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};
export const dockerContainer_postByID = (req: Request, res: Response) => {
(async () => {
try {
const { id } = req.params;
const { name, command }: { name: string; command: string } = req.body;
console.log(name);
const dockerServer = await findDockerInstance(name);

const container = dockerServer.getContainer(id);

switch (command) {
case "start":
await container.start();
break;
case "stop":
await container.stop();
break;
case "pause":
await container.pause();
break;
case "unpause":
await container.unpause();
break;
case "restart":
await container.restart();
break;
default:
throw new Error("Command: " + command + ", is invalid.");
}

res.status(200).json(`${command} command have been sent to the container`);
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};

export const dockerContainer_post = (req: Request, res: Response) => {
(async () => {
try {
res.status(200).json("Post response");
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};

export const dockerContainer_put = (req: Request, res: Response) => {
(async () => {
try {
res.status(200).json("Put response");
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(400).send("Something went wrong.");
}
})();
};

export const dockerContainer_delete = (req: Request, res: Response) => {
(async () => {
try {
res.status(200).json("Delete response");
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};
19 changes: 19 additions & 0 deletions src/controllers/serverControllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Request, Response } from "express";
import { prisma } from "../db/client";
import { findDockerInstance } from "../utils/docker/findDockerInstance";
import { gertContainersList } from "../utils/docker/gertContainersList";
import { getCombinedContainerlist } from "../utils/docker/getCombinedContainerlist";

export const servers_get = (req: Request, res: Response) => {
(async () => {
Expand All @@ -16,6 +18,23 @@ export const servers_get = (req: Request, res: Response) => {
})();
};

export const servers_getByName = (req: Request, res: Response) => {
(async () => {
try {
const { name } = req.params;
console.log(name);
const dockerServer = await findDockerInstance(name);

const allContainers = await getCombinedContainerlist();
allContainers && res.status(200).json(dockerServer);
} catch (err) {
await prisma.$disconnect();
console.log(`Endpoint requested: ${req.originalUrl}`, (err as Error).message);
res.status(404).send("Something went wrong.");
}
})();
};

export const server_post = (req: Request, res: Response) => {
(async () => {
try {
Expand Down
19 changes: 19 additions & 0 deletions src/routes/dockerContainerRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Router } from "express";
import {
dockerContainer_delete,
dockerContainer_getAll,
dockerContainer_getByID,
dockerContainer_getByServer,
dockerContainer_post,
dockerContainer_postByID,
dockerContainer_put,
} from "../controllers/dockerContainerControllers";

export const dockerContainerRoute = Router();
dockerContainerRoute.get("/", dockerContainer_getAll);
dockerContainerRoute.get("/:id", dockerContainer_getByID);
dockerContainerRoute.post("/:id", dockerContainer_postByID);
dockerContainerRoute.get("/server/:name", dockerContainer_getByServer);
dockerContainerRoute.post("/", dockerContainer_post);
dockerContainerRoute.put("/:id", dockerContainer_put);
dockerContainerRoute.delete("/:id", dockerContainer_delete);
9 changes: 8 additions & 1 deletion src/routes/serverRoute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Router } from "express";
import { server_delete, server_post, server_put, servers_get } from "../controllers/serverControllers";
import {
server_delete,
server_post,
server_put,
servers_get,
servers_getByName,
} from "../controllers/serverControllers";

export const serverRouter = Router();
serverRouter.get("/", servers_get);
serverRouter.get("/:name", servers_getByName);
serverRouter.post("/", server_post);
serverRouter.put("/:id", server_put);
serverRouter.delete("/:id", server_delete);
8 changes: 8 additions & 0 deletions src/utils/docker/createDockerServers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { prisma } from "../../db/client";
import { createDockerServersInstances } from "./createDockerServcersInstances";

export async function createDockerServers() {
let servers = await prisma.containerserver.findMany();
let dockerServersInstances = createDockerServersInstances(servers);
return dockerServersInstances;
}
8 changes: 8 additions & 0 deletions src/utils/docker/findDockerInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createDockerServers } from "./createDockerServers";

export async function findDockerInstance(name: string) {
let dockerServersInstances = await createDockerServers();

const indexOfServer = dockerServersInstances.findIndex((server: any) => server.modem.headers?.name === name);
return dockerServersInstances[indexOfServer];
}
Loading

0 comments on commit bdc6df1

Please sign in to comment.