forked from opentok/opentok-meet-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (84 loc) · 2.8 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
const express = require('express');
const fs = require('fs');
const OpenTok = require('opentok');
const https = require('https');
const compression = require('compression');
const redis = require('redis');
const url = require('url');
const glob = require('glob');
const path = require('path');
const cookieSession = require('cookie-session');
const passport = require('passport');
const initGoogleAuth = require('./server/auth/google-auth');
const app = express();
let config;
if (process.env.HEROKU || process.env.TRAVIS) {
config = {
baseUrl: process.env.BASE_URL,
port: process.env.PORT,
apiKey: process.env.OT_API_KEY,
apiSecret: process.env.OT_API_SECRET,
chromeExtensionId: process.env.CHROME_EXTENSION_ID,
apiUrl: process.env.OT_API_URL || 'https://api.dev.opentok.com',
opentokJs: process.env.OT_JS_URL || 'https://www.dev.tokbox.com/v2/js/opentok.js',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
sessionHash: process.env.SESSION_HASH,
};
} else {
try {
config = JSON.parse(fs.readFileSync('./config.json'));
} catch (err) {
console.log('Error reading config.json - have you copied config.json.sample to config.json? ', err);
process.exit();
}
}
if (config.clientId) {
initGoogleAuth(config);
}
let redisClient;
if (process.env.REDISTOGO_URL) {
const rtg = url.parse(process.env.REDISTOGO_URL);
redisClient = redis.createClient(rtg.port, rtg.hostname);
redisClient.auth(rtg.auth.split(':')[1]);
} else if (process.env.REDIS_URL) {
redisClient = redis.createClient(process.env.REDIS_URL);
} else {
redisClient = redis.createClient();
}
app.use(cookieSession({
name: 'meet-session',
keys: [config.sessionHash],
}));
app.use(compression());
app.use(express.logger());
app.use(passport.initialize());
app.use(passport.session());
app.configure(() => {
app.set('views', `${__dirname}/views`);
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(`${__dirname}/public`));
app.use(app.router);
});
const ot = new OpenTok(config.apiKey, config.apiSecret, config.apiUrl);
const useSSL = fs.existsSync(`${__dirname}/server.key`) &&
fs.existsSync(`${__dirname}/server.crt`);
require('./server/routes.js')(app, config, redisClient, ot, useSSL || process.env.HEROKU);
glob.sync('./plugins/**/*.js').forEach((file) => {
// eslint-disable-next-line
require(path.resolve(file))(app, config, redisClient, ot);
});
if (process.env.HEROKU || !useSSL) {
app.listen(config.port, () => {
console.log(`Listening on ${config.port}`);
});
} else {
https.createServer({
key: fs.readFileSync('./server.key', 'utf8'),
cert: fs.readFileSync('./server.crt', 'utf8'),
}, app).listen(config.port, () => {
console.log(`Listening on ${config.port}`);
});
}