forked from opentok/opentok-webinar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (88 loc) · 3.28 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
/**
* Tokinar main server script
*
* This script starts the Tokinar application server by mounting all
* the necessary routes, loading configuration and creating a handler
* to OpenTok's server side SDK.
*/
// Load dependencies -----------------------------
const express = require("express");
const opentok = require("opentok");
const bodyparser = require("body-parser");
const cookies = require("cookie-parser");
const csrf = require("csurf");
const storage = require("./libs/storage");
const utils = require("./libs/utils");
const msgs = require("./libs/messages");
// Load config from file & merge with env vars ---
let config = utils.load_config(process.env.PWD);
// Setup OpenTok ---------------------------------
const OT = new opentok(config.opentok.api_key, config.opentok.api_secret);
// Setup storage
let db = new storage(config.app.storage_dir);
// Create app instance ---------------------------
let app = express();
// Set view engine -------------------------------
app.set("view engine", "ejs");
// Enable body-parser ----------------------------
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
// Enable cookie-parser --------------------------
app.use(cookies());
// Enable CSRF
app.use(csrf({ cookie: true }));
// Security measures -----------------------------
app.disable("x-powered-by");
// Mount middlewares -----------------------------
app.use((req, res, next) => {
req.config = config; // Add config
req.OT = OT; // Add OpenTok SDK instance
req.db = db; // Add db connection
req.utils = utils; // Add utility functions
req.template_data = { // Set data for templates
title: "Tokinar", // Default page title
csrf: null, // Use req.csrfToken()
error: msgs.from_query("error", req.query.e), // Error message
info: msgs.from_query("info", req.query.i), // Info message
scripts: ["libs/clipboard", "tokinar"], // List of scripts to load
styles: ["tokinar"], // List of styles to load
ga: config.app.ga || null // Google Analytics code
};
next();
});
// Mount routes ----------------------------------
app.use("/", require("./routes/home"));
// Mount webinar routes
app.use("/webinar", require("./routes/webinar"));
// Mount the `./assets` dir as static.
app.use("/assets", express.static("./assets"));
// Handle errors ---------------------------------
app.use((req, res) => {
req.template_data.title = "Gone! Not found!";
res.status(404).render("404", req.template_data);
});
app.use((err, req, res, next) => {
console.log("Error", err);
req.template_data.title = "OOPS!";
res.status(500).render("500", req.template_data);
});
// Start server ----------------------------------
let port = config.app.port || 8080;
if (!config.ssl.enabled) {
// Start as Non-ssl
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
} else {
// Start as SSL
const https = require("https");
const fs = require("fs");
const https_options = {
key: fs.readFileSync(config.ssl.key),
cert: fs.readFileSync(config.ssl.cert),
passphrase: config.ssl.passphrase
};
https.createServer(https_options, app).listen(port, () => {
console.log(`Listening on secure port ${port}...`);
});
}