setup routing

This commit is contained in:
Gabriel Matte 2024-11-10 16:33:45 -05:00
parent c26708a609
commit 80115f050c
4 changed files with 57 additions and 14 deletions

View file

@ -1,9 +1,13 @@
const {Room} = require('../models/room.js');
const BaseRoomProvider = require('../roomsProviders/base-provider.js');
//const ClusterRoomProvider = require('../roomsProviders/cluster-provider.js');
const DockerRoomProvider = require('../roomsProviders/docker-provider.js');
//const KubernetesRoomProvider = require('../roomsProviders/kubernetes-provider');
const NB_CODE_CHARS = 6;
const DEFAULT_HOST = "localhost:4500"
class RoomsController {
constructor(options = {}, roomRepository) {
this.provider = this.createProvider(
@ -40,8 +44,19 @@ class RoomsController {
}
async createRoom(options = {}) {
const roomId = options.roomId || this.generateRoomId();
return await this.provider.createRoom(roomId, options);
let roomIdValid = false
let roomId;
while(!roomIdValid){
roomId = options.roomId || this.generateRoomId();
roomIdValid = !(await this.provider.getRoomInfo(roomId));
}
return await this.roomRepository.create(new Room(roomId, roomId, DEFAULT_HOST, 0));
}
async updateRoom(roomId, info) {
return await this.provider.updateRoomInfo(roomId, {});
}
async deleteRoom(roomId) {
@ -57,7 +72,14 @@ class RoomsController {
}
generateRoomId() {
return Math.random().toString(36).substring(2, 7);
const characters = "0123456789";
let result = "";
for (let i = 0; i < NB_CODE_CHARS; i++) {
result += characters.charAt(
Math.floor(Math.random() * characters.length)
);
}
return result;
}
}

View file

@ -29,8 +29,8 @@ class RoomRepository{
if (existingRoom) {
throw new Error(`Room already exists with id: ${room.id}`);
}
const result = await this.collection.insertOne(room);
return result.insertedId;
const returnedId = await this.collection.insertOne(room);
return await this.collection.findOne({ _id: returnedId.insertedId });
}
async get(id) {

View file

@ -17,6 +17,7 @@ class DockerRoomProvider extends BaseRoomProvider {
}
async deleteRoom(roomId) {
return await this.roomRepository.delete(roomId); // shortcircuit -- not implemented yet
try {
const container = this.docker.getContainer(roomId);
await container.stop();
@ -34,9 +35,11 @@ class DockerRoomProvider extends BaseRoomProvider {
}
async getRoomStatus(roomId) {
const room = await roomRepository.get(roomId);
const room = await this.roomRepository.get(roomId);
if (!room) return null;
return room; // shortcircuit -- not implemented yet
try {
const container = this.docker.getContainer(room.containerId);
const info = await container.inspect();

View file

@ -4,7 +4,7 @@ const jwt = require('../middleware/jwtToken.js');
const router = Router();
router.get("/listRooms", async (req, res)=> {
router.get("/", async (req, res)=> {
try {
const data = await roomsController.listRooms();
res.json(data);
@ -14,30 +14,48 @@ router.get("/listRooms", async (req, res)=> {
});
router.get("/createRoom", async (req, res) => {
router.post("/", async (req, res) => {
try {
const data = await roomsController.createRoom();
res.json(data);
} catch (error) {
res.status(500).json({ error: "Failed to list rooms" });
res.status(500).json({ error: "Failed to create room" });
}
});
router.get("/deleteRoom", async (req, res) => {
router.put("/:id", async (req, res) => {
try {
const data = await roomsController.deleteRoom();
const data = await roomsController.updateRoom(req.params.id);
res.json(data);
} catch (error) {
res.status(500).json({ error: "Failed to list rooms" });
res.status(500).json({ error: "Failed to update rooms" });
}
});
router.get("/getRoomStatus", async (req, res) => {
router.delete("/:id", async (req, res) => {
try {
const data = await roomsController.deleteRoom(req.params.id);
res.json(data);
} catch (error) {
res.status(500).json({ error: `Failed to delete room` });
}
});
router.get("/:id", async (req, res) => {
try {
const data = await roomsController.getRoomStatus();
res.json(data);
} catch (error) {
res.status(500).json({ error: "Failed to list rooms" });
res.status(500).json({ error: "Failed to join rooms" });
}
});
router.get("/:id/status", async (req, res) => {
try {
const data = await roomsController.getRoomStatus(req.params.id);
res.json(data);
} catch (error) {
res.status(500).json({ error: "Failed to list room infos" });
}
});