generated from phazonoverload/vonage-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can create sessions using Netlify Functions
- Loading branch information
Kevin Lewis
committed
Oct 2, 2020
1 parent
3b567be
commit 5a48e80
Showing
5 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Local Netlify folder | ||
.netlify | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
exports.handler = async event => { | ||
try { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'ok' }) | ||
} | ||
} catch (err) { | ||
console.error(err.String()) | ||
return { statusCode: 500, body: err.toString() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const OpenTok = require('opentok') | ||
const OT = new OpenTok(process.env.VIDEO_KEY, process.env.VIDEO_SECRET) | ||
const MongoClient = require('mongodb').MongoClient | ||
const client = new MongoClient(process.env.MONGODB_URL, { useNewUrlParser: true, useUnifiedTopology: true }) | ||
|
||
exports.handler = async event => { | ||
try { | ||
await client.connect() | ||
const sessions = client.db('bat').collection('sessions') | ||
let session = await sessions.findOne({ friendly: event.queryStringParameters.room }) | ||
|
||
if (!session) { | ||
const newSession = { sessionId: await createSession(), friendly: event.queryStringParameters.room } | ||
await sessions.insertOne(newSession) | ||
session = newSession | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
sessionId: session.sessionId, | ||
apiKey: process.env.VIDEO_KEY, | ||
token: OT.generateToken(session.sessionId, { role: 'publisher' }) | ||
}) | ||
} | ||
} catch (err) { | ||
console.error(String(err)) | ||
return { statusCode: 500, body: String(err) } | ||
} | ||
} | ||
|
||
function createSession() { | ||
return new Promise((resolve, reject) => { | ||
OT.createSession((err, session) => { | ||
if (err) reject(err) | ||
resolve(session.sessionId) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[build] | ||
functions = "functions" | ||
[[redirects]] | ||
from = "/api/*" | ||
to = "/.netlify/functions/:splat" | ||
status = 200 |
Oops, something went wrong.