Compare commits
6 commits
131ba0869f
...
9f5d226309
Author | SHA1 | Date | |
---|---|---|---|
9f5d226309 | |||
85fe52d851 | |||
3fe53c7bc2 | |||
9413bf7575 | |||
d30942078c | |||
9611504e17 |
2 changed files with 19 additions and 11 deletions
|
@ -54,9 +54,11 @@ export class DockerClient {
|
||||||
}
|
}
|
||||||
throw new Error("Too many container for the order " + orderId + " found");
|
throw new Error("Too many container for the order " + orderId + " found");
|
||||||
}
|
}
|
||||||
const container = containers[0];
|
const containerInfo = containers[0];
|
||||||
|
const containerId = containerInfo.Id;
|
||||||
|
const container = await this.#dockerConnection.getContainer(containerId);
|
||||||
|
|
||||||
await container.stop();
|
await container.stop({t: 10});
|
||||||
await container.remove();
|
await container.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
src/index.js
24
src/index.js
|
@ -1,6 +1,7 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import { Docker } from "./docker-helper.cjs";
|
import { Docker } from "./docker-helper.cjs";
|
||||||
import { DockerClient } from "./docker.js";
|
import { DockerClient } from "./docker.js";
|
||||||
|
import * as fs from "node:fs"
|
||||||
|
|
||||||
const { dockerClient, app } = init();
|
const { dockerClient, app } = init();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
@ -20,13 +21,14 @@ app.post('/', async (req, res) => {
|
||||||
res.send('Hello World!');
|
res.send('Hello World!');
|
||||||
})
|
})
|
||||||
|
|
||||||
app.route("/api/v1")
|
app.route("/api/v1/order/:id")
|
||||||
// create a new deployment
|
// create a new deployment
|
||||||
.post('/order/:id', async (req, res) => {
|
.post(async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const queryParams = req.query;
|
const queryParams = req.query;
|
||||||
if (queryParams.userId == null || queryParams.orderId == null) {
|
if (queryParams.userId == null || queryParams.productId == null) {
|
||||||
res.status(400).statusMessage("Missing query params. Required: userId, productId")
|
res.status(400).send("Missing query params. Required: userId, productId")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
const params = req.params;
|
const params = req.params;
|
||||||
await dockerClient.start(queryParams.userId, queryParams.productId, params.id);
|
await dockerClient.start(queryParams.userId, queryParams.productId, params.id);
|
||||||
|
@ -38,11 +40,16 @@ app.route("/api/v1")
|
||||||
return;
|
return;
|
||||||
})
|
})
|
||||||
// delete an order
|
// delete an order
|
||||||
.delete('/order/:id', async (req, res) => {
|
.delete(async (req, res) => {
|
||||||
try {
|
|
||||||
const params = req.params;
|
const params = req.params;
|
||||||
await dockerClient.delete(params.id);
|
const orderId = params.id;
|
||||||
|
try {
|
||||||
|
await dockerClient.delete(orderId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e.message.includes("no such") || e.message.includes("No container")) {
|
||||||
|
res.status(404).send("No order found with id: " + orderId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
res.status(500).send(e.message);
|
res.status(500).send(e.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -58,10 +65,9 @@ app.listen(port, () => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the initialization logic.
|
* Contains the initialization logic.
|
||||||
* @param {Dockerode} dockerClient
|
|
||||||
* @returns {Express} express session
|
* @returns {Express} express session
|
||||||
*/
|
*/
|
||||||
function init(dockerClient) {
|
function init() {
|
||||||
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
|
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
|
||||||
var stats = fs.statSync(socket);
|
var stats = fs.statSync(socket);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue