This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
187 lines (174 loc) · 6.1 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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 = '0.0.0.0';
let port = 8080;
let protocol = 'https';
var ssl_server_key = 'server.key';
var ssl_server_crt = 'server.crt';
let options = {
key: fs.readFileSync(ssl_server_key),
cert: fs.readFileSync(ssl_server_crt)
};
const chime = new AWS.Chime({ region: 'us-east-1' });
const alternateEndpoint = process.env.ENDPOINT;
if (alternateEndpoint) {
console.log('Using endpoint: ' + alternateEndpoint);
chime.createMeeting({ ClientRequestToken: uuid() }, () => { });
AWS.NodeHttpClient.sslAgent.options.rejectUnauthorized = false;
chime.endpoint = new AWS.Endpoint(alternateEndpoint);
} else {
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 server = require(protocol).createServer(options, async (request, response) => {
log(`${request.method} ${request.url} BEGIN`);
compression({})(request, response, () => { });
try {
if(request.url.startsWith('/attendee?')){
if(request.method === 'GET'){
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.url.startsWith('/meeting?')){
if(request.method === 'GET'){
}else if (request.method === 'POST'){
const query = url.parse(request.url, true).query;
const title = query.title;
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,
},
};
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.url.startsWith('/end?')){
if(request.method === 'GET'){
}else if (request.method === 'POST'){
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 if(request.url.startsWith('/logs')){
if(request.method === 'GET'){
}else if (request.method === 'POST'){
console.log('Writing logs to cloudwatch');
response.end('Writing logs to cloudwatch');
}
}else if(request.url.startsWith('/join?')){
if(request.method === 'GET'){
}else if (request.method === 'POST'){
const query = url.parse(request.url, true).query;
const title = query.title;
const name = query.name;
const res = await chime.listMeetings().promise();
let meeting_exists = false
for (let i = 0; i < res.Meetings.length; i++) {
if (!meetingCache[title]) {
break
}
if (meetingCache[title].Meeting.MeetingId === res.Meetings[i].MeetingId) {
meeting_exists = true
}
}
if (!meeting_exists) {
// 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'){
console.log(request.url)
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html');
if (request.url === '/' || request.url.startsWith('/?')) {
response.end(fs.readFileSync(`build/index.html`));
} else {
response.end(fs.readFileSync(`build/${request.url}`));
}
}
}
} 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}/`);
});