-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.js
156 lines (139 loc) · 5.07 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
"use strict";
var Express = require('express');
var http = require('http');
var https = require('https');
var path = require('path');
var fs = require('fs');
var qs = require('querystring');
const TARGET_API = process.env.TARGET_API || 'ovp';
const STATIC_DIR = __dirname + '/generated/' + TARGET_API;
const DEFAULT_INDEX = fs.readFileSync(path.join(STATIC_DIR, 'default_index.html'), 'utf8');
const BASE_PATH = process.env.BASE_PATH || '';
var App = Express();
App.use(require('compression')());
if (process.env.ENABLE_CROSS_ORIGIN) {
App.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
}
if (process.env.USE_BASIC_AUTH && process.env.LUCYBOT_USERNAME && process.env.LUCYBOT_PASSWD) {
App.use(require('./routes/basic-auth.js'));
}
App.use('/homepage', Express.static(__dirname + '/homepage'));
App.engine('pug', require('pug').__express);
App.set('view engine', 'pug')
App.set('views', __dirname + '/homepage/views');
if (process.env.ENABLE_HOMEPAGE === 'true') {
App.get('/', (req, res) => {
res.render('index', {}, (err, html) => {
if (err) res.status(500).send(err.toString());
res.send(html);
});
})
}
App.get('/player', (req, res) => {
res.render('player-index', {}, (err, html) => {
if (err) res.status(500).send(err.toString());
res.send(html);
});
})
App.get('/vpaas_for_devs', (req, res) => {
res.render('marketing-index', {utm_source: req.query.utm_source,utm_campaign:req.query.utm_campaign, utm_medium:req.query.utm_medium}, (err, html) => {
if (err) res.status(500).send(err.toString());
res.send(html);
});
})
if (!process.env.DEVELOPMENT || process.env.USE_CACHE) {
var cache = function(age) {
age = age || 'med';
if (age === 'short') age = 60 * 60;
if (age === 'med') age = 60 * 60 * 24;
if (age === 'long') age = 60 * 60 * 24 * 30;
return function(req, res, next) {
res.setHeader('Cache-Control', 'public, max-age=' + age);
next();
};
}
App.use(BASE_PATH + '/fonts', cache('long'));
App.use(BASE_PATH + '/img', cache('long'));
App.use(BASE_PATH + '/workflows', cache('med'));
App.use(BASE_PATH + '/kaltura_static', cache('med'));
App.use(BASE_PATH + '/minified', cache('med'));
App.use(BASE_PATH + '/partials', cache('med'));
App.use(BASE_PATH + '/swagger.js', cache('med'));
App.use(BASE_PATH + '/swagger.json', cache('med'));
}
var recipeRedirects = require('./recipe-redirects');
App.use(BASE_PATH + '/recipes/:recipe/embed', (req, res, next) => {
var redirect = recipeRedirects[req.params.recipe];
if (!redirect) return next();
res.redirect(redirect.redirect + '?embed=true');
});
var v4Redirects = require('./v4-redirects');
v4Redirects.forEach(redirect => {
App.get(redirect.from, (req, res, next) => {
if (req.originalUrl !== redirect.from) return next();
res.redirect(301, redirect.to);
})
})
App.get('/api-docs', (req, res) => {
let url = req.protocol + '://' + req.get('host') + '/api-docs/Overview';
if (Object.keys(req.query).length) {
url += '?' + qs.stringify(req.query);
}
res.redirect(url);
})
if (BASE_PATH) {
App.use(BASE_PATH, Express.static(STATIC_DIR));
} else {
App.use(Express.static(STATIC_DIR));
}
const GH_PAGES_BASE = BASE_PATH + '/kaltura-recipes-v3';
App.use(GH_PAGES_BASE, (req, res, next) => {
res.redirect(req.originalUrl.substring(GH_PAGES_BASE.length));
})
App.use(BASE_PATH + '/quiz', require('./routes/quiz'))
App.use(BASE_PATH + '/discussion', require('./routes/discussion'));
if (process.env.TARGET_API === 'ott') {
App.use(BASE_PATH + '/auth', require('./routes/ott-auth.js'));
} else {
App.use(BASE_PATH + '/auth', require('./routes/partner-auth.js'));
}
App.use(BASE_PATH + '/github', require('./routes/github.js'));
App.get('*', (req, res) => {
if (req.originalUrl.endsWith('.html/')) {
let newUrl = req.protocol + '://' + req.get('host') + req.originalUrl.replace(/\/$/, '');
return res.redirect(301, newUrl);
}
res.set('Content-Type', 'text/html');
res.send(DEFAULT_INDEX);
})
if (process.env.DEVELOPMENT || process.env.NO_SSL) {
console.log('----DEVELOPMENT ENVIRONMENT----');
var port = process.env.KALTURA_RECIPES_PORT || 3000;
console.log('listening on port ' + port);
App.listen(port);
} else {
var port = process.env.KALTURA_RECIPES_PORT || 443
var sslOptions = {
key: fs.readFileSync('/etc/ssl/certs/kaltura.org.key'),
cert: fs.readFileSync('/etc/ssl/certs/kaltura.org.crt'),
ca: fs.readFileSync('/etc/ssl/certs/ca-kaltura.org.crt'),
requestCert: true,
rejectUnauthorized: false
};
var secureServer = https.createServer(sslOptions, App).listen(port, function(){
console.log("Secure Express server listening on port "+port);
});
// Redirect from http port 80 to https
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(80);
}
App.use((err, req, res, next) => {
console.error(err);
next();
})