Compare commits

..

6 commits

Author SHA1 Message Date
9f5d226309 Fix returning wrong status if container is not found on delete
Kinda hacky to check if the error message contains a hint for the case.
2022-12-22 17:31:04 +01:00
85fe52d851 Fix stop() not being a function
We need to obtain a Container object to use the function.
2022-12-22 17:30:23 +01:00
3fe53c7bc2 Fix checking for wrong query param on order create 2022-12-22 17:27:12 +01:00
9413bf7575 Fix express route building
Misinterpreted the documentation. It is just a way to group methods
with the same path...
2022-12-22 17:07:20 +01:00
d30942078c Remove param to enable defining a dockerClient inside the scope 2022-12-22 17:04:57 +01:00
9611504e17 Add missing fs import 2022-12-22 17:04:21 +01:00
2 changed files with 19 additions and 11 deletions

View file

@ -54,9 +54,11 @@ export class DockerClient {
}
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();
}

View file

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