-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
315 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
})(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
Oops, something went wrong.