Compare commits

..

No commits in common. "9f5d2263096ee44c534b1f1acb2ad24df800d13d" and "131ba0869ff74c6b5511378fdaf31b81d3904e12" have entirely different histories.

2 changed files with 11 additions and 19 deletions

View file

@ -54,11 +54,9 @@ export class DockerClient {
}
throw new Error("Too many container for the order " + orderId + " found");
}
const containerInfo = containers[0];
const containerId = containerInfo.Id;
const container = await this.#dockerConnection.getContainer(containerId);
const container = containers[0];
await container.stop({t: 10});
await container.stop();
await container.remove();
}

View file

@ -1,7 +1,6 @@
import express from "express";
import { Docker } from "./docker-helper.cjs";
import { DockerClient } from "./docker.js";
import * as fs from "node:fs"
const { dockerClient, app } = init();
const port = 3000;
@ -21,14 +20,13 @@ app.post('/', async (req, res) => {
res.send('Hello World!');
})
app.route("/api/v1/order/:id")
app.route("/api/v1")
// create a new deployment
.post(async (req, res) => {
.post('/order/:id', async (req, res) => {
try {
const queryParams = req.query;
if (queryParams.userId == null || queryParams.productId == null) {
res.status(400).send("Missing query params. Required: userId, productId")
return
if (queryParams.userId == null || queryParams.orderId == null) {
res.status(400).statusMessage("Missing query params. Required: userId, productId")
}
const params = req.params;
await dockerClient.start(queryParams.userId, queryParams.productId, params.id);
@ -40,16 +38,11 @@ app.route("/api/v1/order/:id")
return;
})
// delete an order
.delete(async (req, res) => {
const params = req.params;
const orderId = params.id;
.delete('/order/:id', async (req, res) => {
try {
await dockerClient.delete(orderId);
const params = req.params;
await dockerClient.delete(params.id);
} 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);
return;
}
@ -65,9 +58,10 @@ app.listen(port, () => {
/**
* Contains the initialization logic.
* @param {Dockerode} dockerClient
* @returns {Express} express session
*/
function init() {
function init(dockerClient) {
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
var stats = fs.statSync(socket);