Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 8dfeed6

Browse files
committed
feat(serve): Added '/container-status' endpoint for checking if a container is running/stopped
1 parent cc00685 commit 8dfeed6

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Environment.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export enum Platform {
1919
UNIX, WIN, DOCKER
2020
}
2121

22+
export enum ContainerStatus {
23+
RUNNING, STOPPED
24+
}
25+
2226
const DOCKER_DEFAULT_COMMAND = 'sh'
2327
const DOCKER_CONTAINER_ID_SHORT_LENGTH = 12
2428

src/serve.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import express from 'express'
55

66
const jwt = require('express-jwt')
77

8-
import Environment, { Platform, SessionParameters } from './Environment'
8+
import Environment, { ContainerStatus, Platform, SessionParameters } from './Environment'
99

1010
const app = express()
1111
const expressWs = require('express-ws')(app)
@@ -303,10 +303,32 @@ expressWs.app.post('/stop', async (req: express.Request, res: express.Response)
303303
error: `Container ${containerId} was not stopped.`
304304
})
305305
}
306-
307306
})
308307

309-
// TODO: session (container) info querying (i.e. is container still running)
308+
expressWs.app.post('/container-status', async (req: express.Request, res: express.Response) => {
309+
// req: some JSON -> with container ID that will stop the container
310+
if (!doRequestValidation(req, res, ['environmentId', 'containerId'])) {
311+
return res.end()
312+
}
313+
314+
const jwtData = getJwtData(req, res, req.body.containerId)
315+
316+
if (jwtData === null) {
317+
return res.end()
318+
}
319+
320+
const env = new Environment(req.body.environmentId)
321+
const sessionParameters = new SessionParameters()
322+
sessionParameters.platform = Platform.DOCKER
323+
324+
const containerId = req.body.containerId
325+
326+
return res.json(
327+
{
328+
status: ContainerStatus[await env.containerIsRunning(containerId) ? ContainerStatus.RUNNING : ContainerStatus.STOPPED]
329+
}
330+
).end()
331+
})
310332

311333
// Error handling middleware
312334
app.use((error: Error, req: express.Request, res: express.Response, next: any) => {

0 commit comments

Comments
 (0)