[mirotalk] - add mirotalk api v1

This commit is contained in:
Miroslav Pejic
2021-06-12 09:00:47 +02:00
parent 62d4a66ab4
commit 6f2b438454
3 changed files with 93 additions and 4 deletions
+7 -1
View File
@@ -16,4 +16,10 @@ TURN_USERNAME=YourNumbUsername
TURN_PASSWORD=YourNumbPassword
# Heroku
# https://devcenter.heroku.com/articles/config-vars
# https://devcenter.heroku.com/articles/config-vars
# API
# The response will give you a entrypoint / Room URL for your meeting.
# curl -X POST "http://localhost:3000/api/v1/meeting" -H "authorization: YourApiKeySecret" -H "Content-Type: application/json" -d "{ \"title\": \"Mirotalk GET meeting\"}"
API_KEY_SECRET=YourApiKeySecret
+16 -1
View File
@@ -115,13 +115,18 @@ npm start
Install: https://docs.docker.com/compose/install/
```bash
cp .env.template .env
docker-compose up # or
docker-compose up -d
```
- Open http://localhost:3000 in browser
<br>
To `Update` image after some mirotalk `changes` or `updates`.
```bash
docker-compose build
```
To `Stops` containers and removes containers, networks, volumes, and images created by `up`
@@ -129,6 +134,16 @@ To `Stops` containers and removes containers, networks, volumes, and images crea
docker-compose down
```
## API
The `response` will give you a `entrypoint / Room URL` for `your meeting`.
```bash
curl -X POST "http://localhost:3000/api/v1/meeting" -H "authorization: YourApiKeySecret" -H "Content-Type: application/json" -d "{ \"title\": \"Mirotlak GET meeting\"}"
curl -X POST "https://mirotalk.up.railway.app/api/v1/meeting" -H "authorization: mirotalk_default_secret" -H "Content-Type: application/json" -d "{ \"title\": \"Mirotlak GET meeting\"}"
curl -X POST "https://mirotalk.herokuapp.com/api/v1/meeting" -H "authorization: mirotalk_default_secret" -H "Content-Type: application/json" -d "{ \"title\": \"Mirotlak GET meeting\"}"
```
---
If you want to use a client on another computer/network, make sure you publish your server on an `HTTPS` connection.
+70 -2
View File
@@ -31,6 +31,7 @@ const { Server } = require("socket.io");
const io = new Server().listen(server);
const ngrok = require("ngrok");
var API_KEY_SECRET = process.env.API_KEY_SECRET || "mirotalk_default_secret";
var PORT = process.env.PORT || 3000; // signalingServerPort
var localHost = "http://localhost:" + PORT; // http
var channels = {}; // collect channels
@@ -47,8 +48,19 @@ var turnCredential = process.env.TURN_PASSWORD;
// Use all static files from the www folder
app.use(express.static(path.join(__dirname, "www")));
// Remove trailing slashes in url
app.use(function (req, res, next) {
// Api parse body data as json
app.use(express.json());
// Remove trailing slashes in url handle bad requests
app.use(function (err, req, res, next) {
if (err instanceof SyntaxError && err.status === 400 && "body" in err) {
logme("Request Error", {
header: req.headers,
body: req.body,
error: err.message,
});
return res.status(400).send({ status: 404, message: err.message }); // Bad request
}
if (req.path.substr(-1) === "/" && req.path.length > 1) {
let query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
@@ -97,6 +109,60 @@ app.get("/join/*", function (req, res) {
}
});
/**
MIROTALK API v1
The response will give you a entrypoint / Room URL for your meeting.
*/
app.post(["/api/v1/meeting"], (req, res) => {
// check if user was authorized for the api call
let authorization = req.headers.authorization;
if (authorization != API_KEY_SECRET) {
logme("Mirotalk get meeting - Unauthorized", {
header: req.headers,
body: req.body,
});
return res.status(403).json({ error: "Unauthorized!" });
}
// setup mirotalk meeting URL
let host = req.headers.host;
let meetingURL = getMeetingURL(host) + "/join/" + makeId(15);
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ meeting: meetingURL }));
// logme the output if all done
logme("Mirotalk get meeting - Authorized", {
header: req.headers,
body: req.body,
meeting: meetingURL,
});
});
/**
* Get get Meeting Room URL
* @param {*} host string
* @returns meeting Room URL
*/
function getMeetingURL(host) {
return "http" + (host.includes("localhost") ? "" : "s") + "://" + host;
}
/**
* Generate random Id
* @param {*} length int
* @returns random id
*/
function makeId(length) {
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
// end of MIROTALK API v1
/**
* You should probably use a different stun-turn server
* doing commercial stuff, also see:
@@ -135,6 +201,7 @@ async function ngrokStart() {
logme("settings", {
http: localHost,
https: tunnelHttps,
api_key_secret: API_KEY_SECRET,
iceServers: iceServers,
ngrok: {
ngrok_enabled: ngrokEnabled,
@@ -171,6 +238,7 @@ server.listen(PORT, null, function () {
// server settings
logme("settings", {
http: localHost,
api_key_secret: API_KEY_SECRET,
iceServers: iceServers,
});
}