-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmain.js
More file actions
63 lines (56 loc) · 1.83 KB
/
main.js
File metadata and controls
63 lines (56 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import AppwriteService from "./appwrite.js";
import LivekitService from "./livekit.js";
import RoomCreationService from "./room-creation-service.js";
import { throwIfMissing } from "./utils.js";
export default async ({ req, res, log, error }) => {
// Validate environment variables
throwIfMissing(process.env, [
"APPWRITE_API_KEY",
"MASTER_DATABASE_ID",
"ROOMS_COLLECTION_ID",
"LIVEKIT_HOST",
"LIVEKIT_API_KEY",
"LIVEKIT_API_SECRET",
"LIVEKIT_SOCKET_URL",
]);
// Initialize services
const appwriteService = new AppwriteService();
const livekitService = new LivekitService();
const roomCreationService = new RoomCreationService(
appwriteService,
livekitService,
process.env.LIVEKIT_SOCKET_URL
);
// Parse and validate request body
let requestBody;
try {
requestBody = JSON.parse(req.body);
throwIfMissing(requestBody, ["name", "adminUid", "tags"]);
} catch (err) {
error(err.message);
return res.json({ msg: err.message }, 400);
}
// Handle room creation
try {
log(req);
const { name, description, adminUid, tags } = requestBody;
// Delegate business logic to the service
const result = await roomCreationService.createRoom({
name,
description,
adminUid,
tags,
});
log(`Room created: ${result.appwriteRoomId}`);
// Format and return response
return res.json({
msg: "Room created Successfully",
livekit_room: result.livekitRoom,
livekit_socket_url: result.livekitSocketUrl,
access_token: result.accessToken,
});
} catch (e) {
error(String(e));
return res.json({ msg: "Room creation failed" }, 500);
}
};