import { Ctrl, HttpErr, route_gen, ContentTypeEnum, } from "../../HttpServer"; import { ve_service } from "../services"; import { HttpClient } from "../../HttpClient"; const client = new HttpClient({ socket_path: '/var/run/docker.sock', }); export default class ContainerCtrl extends Ctrl { "GET /containers" = () => { const [route, bind_route] = route_gen(); bind_route(async () => { const res = await client.get('/containers/json', { sp: { all: true, }, }); return res.data || []; }); return route; } "GET /containers/{id_or_name}" = () => { const [route, bind_route] = route_gen(); bind_route(async (req) => { const {id_or_name} = req.p_params; return ve_service.containers_find_by_id_or_name(id_or_name); }); return route; } "POST /containers" = () => { const [route, bind_route] = route_gen(); route.req.body.content_type = ContentTypeEnum.JSON; bind_route(async (req) => { try { const container = await ve_service.containers_create(req.p_body); return container; } catch (err: any) { throw new HttpErr({ status_code: err.statusCode || 400, details: err?.json?.message || 'Bad request', }); } }); return route; } "DELETE /containers/{id_or_name}" = () => { const [route, bind_route] = route_gen(); bind_route(async (req) => { const {id_or_name} = req.p_params; return ve_service.containers_delete_by_id_or_name(id_or_name); }); return route; } // "GET /containers/{id_or_name}/start" = () => { // } }