provider create docker quizRoom

This commit is contained in:
MathieuSevignyLavallee 2024-11-10 23:41:03 -05:00
parent 2df750b6f7
commit cca9a2c99a
4 changed files with 39 additions and 18 deletions

View file

@ -18,6 +18,8 @@ services:
container_name: backend container_name: backend
ports: ports:
- "3000:3000" - "3000:3000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment: environment:
PORT: 3000 PORT: 3000
MONGO_URI: "mongodb://mongo:27017/evaluetonsavoir" MONGO_URI: "mongodb://mongo:27017/evaluetonsavoir"

View file

@ -52,7 +52,7 @@ class RoomsController {
roomIdValid = !(await this.provider.getRoomInfo(roomId)); roomIdValid = !(await this.provider.getRoomInfo(roomId));
} }
return await this.roomRepository.create(new Room(roomId, roomId, DEFAULT_HOST, 0)); return await this.provider.createRoom(roomId,options);
} }
async updateRoom(roomId, info) { async updateRoom(roomId, info) {

View file

@ -9,27 +9,48 @@ class DockerRoomProvider extends BaseRoomProvider {
} }
async createRoom(roomId, options) { async createRoom(roomId, options) {
const host = "localhost:4500"; const container_name = `room_${roomId}`;
const id = await this.roomRepository.create(
new Room(roomId, roomId, host, 0) const containerConfig = {
); Image: 'evaluetonsavoir-quizroom', // Your local Docker image name
return roomRepository.get(id); name: container_name,
ExposedPorts: {
"4500/tcp": {}
},
HostConfig: {
PortBindings: {
"4500/tcp": [
{
HostPort: ""
}
]
}
},
Env: options.env || []
};
// Use `this.docker` instead of `docker`
const container = await this.docker.createContainer(containerConfig);
await container.start();
const containerInfo = await container.inspect();
const containerIP = containerInfo.NetworkSettings.IPAddress;
const host = `${containerIP}:4500`;
return await this.roomRepository.create(new Room(roomId, container_name, host, 0));
} }
async deleteRoom(roomId) { async deleteRoom(roomId) {
return await this.roomRepository.delete(roomId); // shortcircuit -- not implemented yet return await this.roomRepository.delete(roomId); // Short-circuit -- not implemented yet
try { try {
const container = this.docker.getContainer(roomId); const container = this.docker.getContainer(roomId);
await container.stop(); await container.stop();
await container.remove(); await container.remove();
await roomRepository.delete(roomId); await this.roomRepository.delete(roomId);
console.log(`Conteneur pour la salle ${roomId} supprimé.`); console.log(`Conteneur pour la salle ${roomId} supprimé.`);
} catch (error) { } catch (error) {
console.error( console.error(`Erreur lors de la suppression du conteneur pour la salle ${roomId}:`, error);
`Erreur lors de la suppression du conteneur pour la salle ${roomId}:`,
error
);
throw new Error("Failed to delete room"); throw new Error("Failed to delete room");
} }
} }
@ -38,7 +59,7 @@ class DockerRoomProvider extends BaseRoomProvider {
const room = await this.roomRepository.get(roomId); const room = await this.roomRepository.get(roomId);
if (!room) return null; if (!room) return null;
return room; // shortcircuit -- not implemented yet return room; // Short-circuit -- not implemented yet
try { try {
const container = this.docker.getContainer(room.containerId); const container = this.docker.getContainer(room.containerId);
@ -58,10 +79,7 @@ class DockerRoomProvider extends BaseRoomProvider {
await this.roomRepository.update(updatedRoomInfo); await this.roomRepository.update(updatedRoomInfo);
return updatedRoomInfo; return updatedRoomInfo;
} catch (error) { } catch (error) {
console.error( console.error(`Erreur lors de la récupération du statut du conteneur pour la salle ${roomId}:`, error);
`Erreur lors de la récupération du statut du conteneur pour la salle ${roomId}:`,
error
);
return null; return null;
} }
} }

View file

@ -19,7 +19,8 @@ router.post("/", async (req, res) => {
const data = await roomsController.createRoom(); const data = await roomsController.createRoom();
res.json(data); res.json(data);
} catch (error) { } catch (error) {
res.status(500).json({ error: "Failed to create room" }); console.log(error);
res.status(500).json({ error: "Failed to create room :" + error });
} }
}); });