This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
app.js
176 lines (152 loc) · 5.11 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
// ***
// *** Required modules
// ***
var express = require('express'),
opentok = require('opentok'),
bodyParser = require('body-parser'), // middleware
cors = require('cors'),
config = require("./config"),
storage = require('./lib/store.js'),
loadMiddleware = require('./lib/load-middleware.js');
// ***
// *** OpenTok Constants for creating Session and Token values
// ***
var OTKEY = config.opentok.key;
var ot = new opentok(config.opentok.key, config.opentok.secret);
// ***
// *** Setup Express to handle static files in public folder
// *** Express is also great for handling url routing
// ***
var app = express();
app.set( 'views', __dirname + "/views");
app.set( 'view engine', 'ejs' );
app.use(bodyParser());
app.use(express.static(__dirname + '/public'));
// ***
// *** Load middleware
// ***
app.use(cors({methods:'GET'}));
storage.init(config); // setup memory or redis, depending on config
loadMiddleware(app, config);
// ***
// *** When user goes to root directory, render index page
// ***
app.get("/", function( req, res ){
res.render('index');
});
// ***
// *** Post endpoint to start/stop archives
// ***
app.post('/archive/:sessionId', function(req, res, next) {
// final function to be called when all the necessary data is gathered
function sendArchiveResponse(error, archive) {
if (error) {
var payload;
if (config.web.env === 'development') {
payload = { error: error.message };
} else {
payload = { error: 'An error occurred, could not ' + req.body.action + ' the archive' };
}
return res.json(500, payload);
}
res.json(archive);
}
// When an archive is given through a reservation
if( req.archiveInfo ){
sendArchiveResponse( req.archiveInfo.error, req.archiveInfo.archive );
return;
}
// When an archive needs to be created or stopped
if( req.body.action === "start" ){
ot.startArchive(req.params.sessionId, {name: req.body.roomId}, sendArchiveResponse);
}else{
ot.stopArchive(req.body.archiveId, sendArchiveResponse);
}
});
// ***
// *** Renders archive page
// ***
app.get('/archive/:archiveId/:roomId', function(req, res, next) {
// final function to be called when all the necessary data is gathered
function sendArchiveResponse(error, archive) {
if (error) {
var payload;
if (config.web.env === 'development') {
payload = { error: error.message };
} else {
payload = { error: 'An error occurred, could not get the archive' };
}
// NOTE: see quirk note below. applies for this property as well.
payload.archive = false;
return res.json(500, payload);
}
// NOTE: sending 'error: false' in the response is unnecessary. this quirk should be removed
// but if clients depend on this behavior then that needs to be changed across the
// clients before making the change.
return res.render('archive', {error: false, archive: archive});
}
// When an archive is given through a reservation
if( req.archiveInfo ){
sendArchiveResponse( req.archiveInfo.error, req.archiveInfo.archive );
return;
}
// When an archive needs to be created or stopped
ot.getArchive(req.params.archiveId, sendArchiveResponse);
});
// ***
// *** When user goes to a room, render the room page
// ***
app.get("/:rid", function( req, res ){
// final function to be called when all the necessary data is gathered
var sendRoomResponse = function(apiKey, sessionId, token) {
var data = {
rid: rid,
sid: sessionId,
sessionId: sessionId,
apiKey : apiKey,
token: token
};
if (req.format === 'json') {
res.json(data);
} else {
res.render('room', data);
}
};
console.log(req.url);
var rid = req.params.rid.split('.json')[0];
var room_uppercase = rid.toUpperCase();
// When a room is given through a reservation
if (req.sessionId && req.apiKey && req.token) {
sendRoomResponse(req.apiKey, req.sessionId, req.token);
} else {
// Check if room sessionId exists. If it does, render response. If not, create sessionId
storage.get(room_uppercase, function(reply){
if(reply){
req.sessionId = reply;
sendRoomResponse(OTKEY, req.sessionId, ot.generateToken(req.sessionId, {role: 'moderator'}));
}else{
ot.createSession( req.sessionProperties || {} , function(err, session){
if (err) {
var payload;
if (config.web.env === 'development') {
payload = { error: err.message };
} else {
payload = { error: 'could not generate opentok session' };
}
return res.send(500, payload);
}
storage.set(room_uppercase, session.sessionId, function(){
sendRoomResponse(OTKEY, session.sessionId, ot.generateToken(session.sessionId, {role: 'moderator'}));
});
});
}
});
}
});
// ***
// *** start server, listen to port (predefined or 9393)
// ***
app.listen(config.web.port, function() {
console.log('application now served on port ' + config.web.port +
' in ' + config.web.env + ' environment');
});