-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
414 lines (382 loc) · 13.1 KB
/
app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Had to convert from CommonJS to ESM.
import express from "express";
import https from "https";
import fs from "fs";
import path from "path";
import { dirname } from "path";
import { MongoClient, ObjectId } from "mongodb";
import atlasUri from "./atlas-uri.js";
import bodyParser from "body-parser";
import session from "express-session";
import { IvsClient, CreateChannelCommand } from "@aws-sdk/client-ivs";
import { fileURLToPath } from "url";
// MongoDB instantiations and declarations, needed for function definitions.
const mongoClient = new MongoClient(atlasUri);
// MongoDB function definitions.
const connectToDatabase = async () => {
try {
await mongoClient.connect();
console.log(
`Connected to the ${dbname} database 🌍 \nFull connection string: ${atlasUri}`
);
} catch (err) {
console.error(`Error connecting to the database: ${err}`);
}
};
const dbname = "live-stream-music";
const collectionName = "users";
const usersCollection = mongoClient.db(dbname).collection(collectionName);
// Used in 'signUpUser()' function.
let userAccount = {
name: "Hugh Wilfred Culling",
email: "[email protected]",
password: "pw123",
};
// Used in 'signUpUser()' and 'retrieveDetails()' functions.
let documentToFind = { email: "[email protected]" };
// IVS instantiations and declarations, needed for 'signUpUser()' function.
const ivsClient = new IvsClient({ region: "eu-west-1" });
// 'name' field will get changed in 'signUpUser()' function.
let ivsChannelMetaData = {
// CreateChannelRequest.
name: "lsm_channel",
latencyMode: "NORMAL",
type: "BASIC",
authorized: false,
recordingConfigurationArn: "",
tags: {
// Tags.
},
insecureIngest: false,
preset: "",
};
// Insert document with user input and automatic ObjectId.
// Create an IVS channel with ObjectId as 'name' and update document with
// 'streamKey' and 'playbackUrl'.
const signUpUser = async () => {
try {
// Check whether email is already associated with an account.
// Insert user-entered email into 'documentToFind'.
documentToFind = { email: `${userAccount.email}` };
console.log(`documentToFind = ${documentToFind}`);
// If email not already used, 'retrieveDetails()' will return "null".
let emailTaken = await retrieveDetails();
console.log(`emailTaken = ${emailTaken}`);
await connectToDatabase();
if (emailTaken == null) {
// 'userAccount' given new values before 'signUpUser()' function call.
let result = await usersCollection.insertOne(userAccount);
console.log(`Inserted document: ${result.insertedId}`);
// 'ivsChannelMetaData' object updated after document inserted to use
// retrieved '_id'.
ivsChannelMetaData.name = `${result.insertedId}`;
console.log(`ivsChannelMetaData.name = ${ivsChannelMetaData.name}`);
// 'command' and 'response' are instantiated and declared here so that
// they receive updated 'ivsChannelMetaData' object.
const command = new CreateChannelCommand(ivsChannelMetaData);
const response = await ivsClient.send(command);
// Had to separate the below logs to avoid indentation.
console.log(
`response.channel.ingestEndpoint = ${response.channel.ingestEndpoint}`
);
console.log(
`response.channel.playbackUrl = ${response.channel.playbackUrl}`
);
console.log(`response.streamKey.value = ${response.streamKey.value}`);
// Update inserted document to include the 'streamKey' and 'playbackUrl'.
// The 'ingestEndpoint' is the same for all my channels in 'eu-west-1'.
const documentToUpdate = { _id: new ObjectId(result.insertedId) };
const update = {
$set: {
streamKey: `${response.streamKey.value}`,
playbackUrl: `${response.channel.playbackUrl}`,
},
};
let updateResult = await usersCollection.updateOne(
documentToUpdate,
update
);
updateResult.modifiedCount > 0
? console.log(`Updated ${updateResult.modifiedCount} documents`)
: console.log("No documents updated");
} else {
console.log("Email already taken");
}
} catch (err) {
console.error(`Error connecting to the database: ${err}`);
} finally {
await mongoClient.close();
}
};
const retrieveDetails = async () => {
try {
await connectToDatabase();
// 'documentToFind' is updated before function call.
let result = await usersCollection.findOne(documentToFind);
console.log(`Found one document`);
// Without 'JSON.stringify()' it would return: "[object Object]".
console.log(`result = ${JSON.stringify(result)}`);
return result;
} catch (err) {
console.error(`Error connecting to the database: ${err}`);
} finally {
await mongoClient.close();
}
};
// Used as parameter in 'res.render()' and 'app.use()' functions to find path
// to Pug files.
const __filename = fileURLToPath(import.meta.url);
console.log(`__filename = ${__filename}`);
// Prints "/root/lsm-ubuntu/app.js".
const __dirname = dirname(__filename);
console.log(`__dirname = ${__dirname}`);
// Prints "/root/lsm-ubuntu".
const app = express();
// Specifies which templating engine to use.
// Allows use of 'res.render()'.
app.set("view engine", "pug");
// Allows serving of static files from 'static' directory.
app.use(express.static(path.join(__dirname, "static")));
// Allows information sent in POST request to be retrieved.
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
session({
resave: false,
saveUninitialized: false,
secret: "secret",
cookie: { secure: false },
})
);
// Returns respective 'broadcastLink' based on whether user is signed in.
function decideBroadcastLink(req) {
// Checks whether a session exists, meaning they would've signed in.
if (req.session.user) {
console.log("decideBroadcastLink() They are signed in.");
console.log(`req.session.user = ${JSON.stringify(req.session.user)}`);
return {
broadcastLink: `/${req.session.user._id}`,
status: `Signed in as: ${req.session.user.name}`,
};
} else {
console.log("decideBroadcastLink() They are not signed in.");
return { broadcastLink: "/sign-in.html", status: "User: not signed in" };
}
}
app.get("/", (req, res) => {
let idPage = decideBroadcastLink(req);
console.log(`idPage = ${JSON.stringify(idPage)}`);
res.render(path.join(__dirname, "views", "index.pug"), {
title: "Live Stream Music",
broadcastLink: `${idPage.broadcastLink}`,
status: `${idPage.status}`,
});
});
app.get("/broadcast.html", (req, res) => {
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "broadcast.pug"), {
title: "Broadcast | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "broadcast.pug"), {
title: "Broadcast | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.get("/playback.html", (req, res) => {
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "playback.pug"), {
title: "Playback | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "playback.pug"), {
title: "Playback | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.get("/sign-up.html", (req, res) => {
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "sign-up.pug"), {
title: "Sign Up | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "sign-up.pug"), {
title: "Sign Up | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.get("/sign-in.html", (req, res) => {
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "sign-in.pug"), {
title: "Sign In | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "sign-in.pug"), {
title: "Sign In | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.get("/sign-out.html", (req, res) => {
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
// Clear session data:
req.session.destroy((err) => {
if (err) {
console.error(err);
return next(err);
}
// Clear the session cookie:
res.clearCookie("connect.sid");
// Redirect to the desired page after sign-out:
res.render(path.join(__dirname, "views", "sign-out.pug"), {
title: "Sign Out | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "sign-out.pug"), {
title: "Sign Out | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.get("/:id", async (req, res) => {
let currentUrl = req.url;
console.log("current URL = " + currentUrl);
let pageid = currentUrl.slice(1, 25);
console.log("page id = " + pageid);
// console.log(pageid == req.session.user._id);
if (req.session.user) {
if (pageid == req.session.user._id) {
console.log("They are the broadcaster");
}
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "user.pug"), {
title: "User | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
streamKey: `${req.session.user.streamKey}`,
});
} else {
console.log("They are not signed in.");
console.log("They are a viewer.");
documentToFind = {
_id: new ObjectId(pageid),
};
let result = await retrieveDetails();
console.log(result);
res.render(path.join(__dirname, "views", "playback.pug"), {
title: "User | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
playbackUrl: `${result.playbackUrl}`,
});
}
});
app.post("/sign-up.html", function (req, res, next) {
userAccount = {
name: `${req.body.name}`,
email: `${req.body.email}`,
password: `${req.body.password}`,
};
signUpUser();
// res.sendFile(path.join(__dirname, "public", "html", "sign-up.html"));
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "sign-up.pug"), {
title: "Sign Up | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "sign-up.pug"), {
title: "Sign Up | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
app.post("/sign-in.html", async function (req, res, next) {
documentToFind = {
email: `${req.body.email}`,
password: `${req.body.password}`,
};
let result = await retrieveDetails();
console.log("They signed in.");
req.session.user = result;
req.session.save();
console.log(result);
// res.sendFile(path.join(__dirname, "public", "html", "sign-in.html"));
if (req.session.user) {
console.log("They are signed in.");
let id = "/" + req.session.user._id;
res.render(path.join(__dirname, "views", "sign-in.pug"), {
title: "Sign In | Live Stream Music",
broadcastLink: `${id}`,
status: `Signed in as: ${req.session.user.name}`,
});
} else {
console.log("They are not signed in.");
res.render(path.join(__dirname, "views", "sign-in.pug"), {
title: "Sign In | Live Stream Music",
broadcastLink: "/sign-in.html",
status: "User: not signed in",
});
}
});
const privateKey = fs.readFileSync(
"/etc/letsencrypt/live/live-stream-music.com/privkey.pem",
"utf8"
);
const certificate = fs.readFileSync(
"/etc/letsencrypt/live/live-stream-music.com/fullchain.pem",
"utf8"
);
const ca = fs.readFileSync(
"/etc/letsencrypt/live/live-stream-music.com/chain.pem",
"utf8"
);
const credentials = {
key: privateKey,
cert: certificate,
ca: ca,
};
const httpsServer = https.createServer(credentials, app);
const PORT = 443;
httpsServer.listen(PORT, () => {
console.log(`Server running on https://live-stream-music.com:${PORT}`);
});