-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
90 lines (78 loc) · 2.32 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
/**
* OpenTok proctoring demo main server script
*/
const express = require('express')
const OpenTok = require('opentok')
const { randomBytes } = require('crypto')
let createId = () => {
var id = randomBytes(3).toString('hex')
return [id.slice(0, 3), id.slice(3)].join('-')
}
// Get configurations
const PORT = process.env.PORT || 8080
const OPENTOK_API_KEY = process.env.OPENTOK_API_KEY
if (!OPENTOK_API_KEY) {
throw new Error('Provide OPENTOK_API_KEY environment variable')
}
const OPENTOK_API_SECRET = process.env.OPENTOK_API_SECRET
if (!OPENTOK_API_SECRET) {
throw new Error('Provide OPENTOK_API_SECRET environment variable')
}
// Bootstrap app
function bootstrap (session) {
// Create expressJS app instance
const app = express()
// Mount the `./web` dir to web-root as static.
app.use('/', express.static('./web'))
app.get('/sessionId', (req, res) => {
res.status(200).json({
sessionId: session.sessionId,
apiKey: OPENTOK_API_KEY
})
})
app.get('/token', (req, res) => {
var id = req.query.id || createId()
try {
const token = OT.generateToken(session.sessionId, {
role: 'publisher',
data: id,
expireTime: Math.round((Date.now() / 1000) + (60 * 60)) // 1 hour from now()
})
res.status(200).json({
apiKey: OPENTOK_API_KEY,
sessionId: session.sessionId,
token: token,
id: id,
role: 'publisher'
})
} catch (e) {
console.log('Error creating token', e)
res.status(500).json({ error: e.message })
}
})
return app
}
// Generate an OpenTok session. Will will use a single session only
const OT = new OpenTok(OPENTOK_API_KEY, OPENTOK_API_SECRET)
OT.createSession({ mediaMode: 'routed' }, (err, session) => {
if (err) {
console.log('Error creating OpenTok session', err)
process.exit(1)
}
if (!process.env.SECURE) {
// Bootstrap and start HTTP server for app
bootstrap(session).listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})
} else {
const https = require('https')
const fs = require('fs')
const tlsOpts = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
}
https.createServer(tlsOpts, bootstrap(session)).listen(PORT, () => {
console.log(`Listening on secure port ${PORT}...`)
})
}
})