forked from aws/amazon-chime-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (105 loc) · 3.78 KB
/
server.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable */
const compression = require('compression');
const fs = require('fs');
const url = require('url');
const uuid = require('uuid/v4');
const AWS = require('aws-sdk');
/* eslint-enable */
let hostname = '127.0.0.1';
let port = 8080;
let protocol = 'http';
let options = {};
const chime = new AWS.Chime({ region: 'us-east-1' });
chime.endpoint = new AWS.Endpoint('https://service.chime.aws.amazon.com/console');
const meetingCache = {};
const attendeeCache = {};
const log = message => {
console.log(`${new Date().toISOString()} ${message}`);
};
const app = process.env.npm_config_app || 'meeting';
const server = require(protocol).createServer(options, async (request, response) => {
log(`${request.method} ${request.url} BEGIN`);
compression({})(request, response, () => {});
try {
if (request.method === 'GET' && (request.url === '/' || request.url.startsWith('/?'))) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html');
response.end(fs.readFileSync(`dist/${app}.html`));
} else if (request.method === 'POST' && request.url.startsWith('/join?')) {
const query = url.parse(request.url, true).query;
const title = query.title;
const name = query.name;
if (!meetingCache[title]) {
meetingCache[title] = await chime
.createMeeting({
ClientRequestToken: uuid(),
// NotificationsConfiguration: {
// SqsQueueArn: 'Paste your arn here',
// SnsTopicArn: 'Paste your arn here'
// }
})
.promise();
attendeeCache[title] = {};
}
const joinInfo = {
JoinInfo: {
Title: title,
Meeting: meetingCache[title].Meeting,
Attendee: (
await chime
.createAttendee({
MeetingId: meetingCache[title].Meeting.MeetingId,
ExternalUserId: uuid(),
})
.promise()
).Attendee,
},
};
attendeeCache[title][joinInfo.JoinInfo.Attendee.AttendeeId] = name;
response.statusCode = 201;
response.setHeader('Content-Type', 'application/json');
response.write(JSON.stringify(joinInfo), 'utf8');
response.end();
log(JSON.stringify(joinInfo, null, 2));
} else if (request.method === 'GET' && request.url.startsWith('/attendee?')) {
const query = url.parse(request.url, true).query;
const attendeeInfo = {
AttendeeInfo: {
AttendeeId: query.attendee,
Name: attendeeCache[query.title][query.attendee],
},
};
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.write(JSON.stringify(attendeeInfo), 'utf8');
response.end();
log(JSON.stringify(attendeeInfo, null, 2));
} else if (request.method === 'POST' && request.url.startsWith('/end?')) {
const query = url.parse(request.url, true).query;
const title = query.title;
await chime
.deleteMeeting({
MeetingId: meetingCache[title].Meeting.MeetingId,
})
.promise();
response.statusCode = 200;
response.end();
} else {
response.statusCode = 404;
response.setHeader('Content-Type', 'text/plain');
response.end('404 Not Found');
}
} catch (err) {
log(`server caught error: ${err}`);
response.statusCode = 403;
response.setHeader('Content-Type', 'application/json');
response.write(JSON.stringify({ error: err.message }), 'utf8');
response.end();
}
log(`${request.method} ${request.url} END`);
});
server.listen(port, hostname, () => {
log(`server running at ${protocol}://${hostname}:${port}/`);
});