forked from polimediaupv/paella-opencast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-server.js
212 lines (162 loc) · 5.28 KB
/
test-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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var express = require('express');
var httpProxy = require('http-proxy');
var https = require('https');
var http = require('http');
var fs = require('fs');
var jsonfile = require('jsonfile');
var useHTTPS = false;
var verbose = true;
if (process.argv.length > 2 && process.argv[2] === '--use-http') {
useHTTPS = false;
}
//var matterhornProxyURL = 'https://matterhorn.dce.harvard.edu/';
var matterhornProxyURL = 'http://10.10.10.50/';
//var matterhornProxyURL = 'http://matterhorn.dev-kdolan.dcex.harvard.edu';
var proxyOpts = {
target: matterhornProxyURL
};
var mostRecentWatchReqUrl;
var cannedEpisode = jsonfile.readFileSync(
// __dirname + '/fixtures/example-episode.json'
// __dirname + '/fixtures/example-live-episode.json'
__dirname + '/fixtures/example-captions-episode.json'
);
var cannedSeries = jsonfile.readFileSync(
__dirname + '/fixtures/example-search-series.json'
);
var cannedMe = jsonfile.readFileSync(
__dirname + '/fixtures/example-me.json'
);
var cannedCaptions = fs.readFileSync(
__dirname + '/fixtures/captions.dfxp'
);
var cannedAnnotations = fs.readFileSync(
__dirname + '/fixtures/example-paella-annotations.json'
);
var cannedVisualAnnotations = fs.readFileSync(
__dirname + '/fixtures/example-paella-annotations-visual.json'
);
var cannedTimedComments = fs.readFileSync(
__dirname + '/fixtures/example-dce-timedcomment.json'
);
var cannedComments = fs.readFileSync(
__dirname + '/fixtures/example-paella-annotations-comments.json'
);
var cannedHeartbeatFootprints = fs.readFileSync(
__dirname + '/fixtures/example-paella-footprint.json'
);
var proxy = httpProxy.createProxyServer({
secure: false
});
var app = express();
var router = express.Router();
// Handle login.html requests with a redirect.
router.get('/login.html*', skipToContent);
router.get('/info/me.json*', me);
// Serve a canned episode for episode requests.
router.get('/search/episode.json*', episode);
// Serve a canned series for series requests.
router.get('/search/series.json*', series);
// Service canned captions
router.get('/caption-file-test-server/captions.dfxp', captions);
// Serve a canned episode for episode requests.
router.get('/annotation/annotations.json*', annotations);
// Serve a canned episode after an annotation new comment update/write
router.put('/annotation/', annotations);
// Serve a canned canAnnotate response
router.get('/annotation/canAnnotate*', canannotate);
// Serve a canned episode after an annotation reply update/write
router.put('/annotation/[0-9]*', annotations);
// Serve a canned footprint for footprint requests.
router.get('/usertracking/footprint.json*', footprint);
// // Quitely consume the usertracking puts
router.get('/usertracking/*', swallow);
router.put('/usertracking/*', swallow);
// // Quitely consume the footprint puts
router.get('/footprint/*', swallow);
// Handle everything else with the proxy back to the Matterhorn server.
router.get('/*', passToProxy);
// Serve /engage/player/* requests from the local build folder.
app.use('/engage/player', express.static('build/paella-opencast'));
app.use('/engage/player', express.static('build/paella-opencast/resources'));
app.use('/engage/player/test_media', express.static('fixtures/test_media'));
app.use('/', router);
function skipToContent(req, res, next) {
log('Skipping to', mostRecentWatchReqUrl);
if (mostRecentWatchReqUrl) {
res.redirect(mostRecentWatchReqUrl);
}
next();
}
function swallow(req, res, next) {
log('Swallowing ' + req.url);
res.end();
}
function episode(req, res) {
log('Serving episode.');
res.json(cannedEpisode);
}
function me(req, res) {
log('Serving me.json.');
res.json(cannedMe);
}
function series(req, res) {
log('Serving search-series.');
res.json(cannedSeries);
}
function captions(req, res) {
log('Serving captions.');
res.header('Content-Type', 'text/xml');
res.end(cannedCaptions);
}
function footprint(req, res) {
log('Serving footprints.');
res.header('Content-Type', 'application/json');
res.end(cannedHeartbeatFootprints);
}
function canannotate(req, res) {
log('Serving canned yes for canAnnotate.');
res.header('Content-Type', 'text/plain');
res.end("true");
}
function annotations(req, res) {
log('Serving annotations, type: ' + req.query.type);
res.header('Content-Type', 'text/json');
if (req.query.type == 'paella/visualAnnotations') {
res.end(cannedVisualAnnotations);
} else if (req.query.type == 'paella/comments') {
res.end(cannedComments);
} else if (req.query.type == 'paella/timedComments') {
res.end(cannedTimedComments);
} else {
// 'paella/annotations' and default all other annotation types
res.end(cannedAnnotations);
}
}
function passToProxy(req, res) {
log('Proxying:', req.url);
if (req.url.indexOf('/engage/player/watch.html') === 0) {
mostRecentWatchReqUrl = req.url;
}
proxy.web(req, res, proxyOpts);
}
var httpsOpts = {
key: fs.readFileSync(__dirname + '/fixtures/test.key'),
cert: fs.readFileSync(__dirname + '/fixtures/test.crt')
};
var server;
if (useHTTPS) {
server = https.createServer(httpsOpts, app);
}
else {
server = http.createServer(app);
}
server.listen(3000);
function log() {
if (verbose) {
console.log.apply(console, arguments);
}
}
log('Listening on port 3000.');
// Needed by test targets in Makefile.
console.log(process.pid);