Compare commits
No commits in common. "9f5d2263096ee44c534b1f1acb2ad24df800d13d" and "131ba0869ff74c6b5511378fdaf31b81d3904e12" have entirely different histories.
9f5d226309
...
131ba0869f
2 changed files with 11 additions and 19 deletions
|
@ -54,11 +54,9 @@ 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 containerInfo = containers[0];
|
const container = containers[0];
|
||||||
const containerId = containerInfo.Id;
|
|
||||||
const container = await this.#dockerConnection.getContainer(containerId);
|
|
||||||
|
|
||||||
await container.stop({t: 10});
|
await container.stop();
|
||||||
await container.remove();
|
await container.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
src/index.js
24
src/index.js
|
@ -1,7 +1,6 @@
|
||||||
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;
|
||||||
|
@ -21,14 +20,13 @@ app.post('/', async (req, res) => {
|
||||||
res.send('Hello World!');
|
res.send('Hello World!');
|
||||||
})
|
})
|
||||||
|
|
||||||
app.route("/api/v1/order/:id")
|
app.route("/api/v1")
|
||||||
// create a new deployment
|
// create a new deployment
|
||||||
.post(async (req, res) => {
|
.post('/order/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const queryParams = req.query;
|
const queryParams = req.query;
|
||||||
if (queryParams.userId == null || queryParams.productId == null) {
|
if (queryParams.userId == null || queryParams.orderId == null) {
|
||||||
res.status(400).send("Missing query params. Required: userId, productId")
|
res.status(400).statusMessage("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);
|
||||||
|
@ -40,16 +38,11 @@ app.route("/api/v1/order/:id")
|
||||||
return;
|
return;
|
||||||
})
|
})
|
||||||
// delete an order
|
// delete an order
|
||||||
.delete(async (req, res) => {
|
.delete('/order/:id', async (req, res) => {
|
||||||
const params = req.params;
|
|
||||||
const orderId = params.id;
|
|
||||||
try {
|
try {
|
||||||
await dockerClient.delete(orderId);
|
const params = req.params;
|
||||||
|
await dockerClient.delete(params.id);
|
||||||
} 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;
|
||||||
}
|
}
|
||||||
|
@ -65,9 +58,10 @@ app.listen(port, () => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the initialization logic.
|
* Contains the initialization logic.
|
||||||
|
* @param {Dockerode} dockerClient
|
||||||
* @returns {Express} express session
|
* @returns {Express} express session
|
||||||
*/
|
*/
|
||||||
function init() {
|
function init(dockerClient) {
|
||||||
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