Update backend tutorials to return JSON response
This commit is contained in:
parent
a55858f7e4
commit
28e6cc206b
@ -25,13 +25,16 @@ public class Controller {
|
||||
* @param params JSON object with roomName and participantName
|
||||
* @return The JWT token
|
||||
*/
|
||||
@PostMapping("/token")
|
||||
@PostMapping(
|
||||
value = "/token",
|
||||
produces = "application/json"
|
||||
)
|
||||
public ResponseEntity<String> getToken(@RequestBody Map<String, String> params) {
|
||||
String roomName = params.get("roomName");
|
||||
String participantName = params.get("participantName");
|
||||
|
||||
if (roomName == null || participantName == null) {
|
||||
return ResponseEntity.badRequest().body("roomName and participantName are required");
|
||||
return ResponseEntity.badRequest().body("\"roomName and participantName are required\"");
|
||||
}
|
||||
|
||||
AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
|
||||
@ -39,7 +42,7 @@ public class Controller {
|
||||
token.setIdentity(participantName);
|
||||
token.addGrants(new RoomJoin(true), new RoomName(roomName));
|
||||
|
||||
return ResponseEntity.ok(token.toJwt());
|
||||
return ResponseEntity.ok("\"" + token.toJwt() + "\"");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,18 +17,18 @@ app.post("/token", async (req, res) => {
|
||||
const participantName = req.body.participantName;
|
||||
|
||||
if (!roomName || !participantName) {
|
||||
res.status(400).send("roomName and participantName are required");
|
||||
res.status(400).json("roomName and participantName are required");
|
||||
return;
|
||||
}
|
||||
|
||||
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
|
||||
identity: participantName,
|
||||
identity: participantName
|
||||
});
|
||||
at.addGrant({ roomJoin: true, room: roomName });
|
||||
const token = await at.toJwt();
|
||||
res.send(token);
|
||||
res.json(token);
|
||||
});
|
||||
|
||||
app.listen(SERVER_PORT, () => {
|
||||
console.log("Application started on port: ", SERVER_PORT);
|
||||
console.log("Server started on port:", SERVER_PORT);
|
||||
});
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
require __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
use Agence104\LiveKit\AccessToken;
|
||||
use Agence104\LiveKit\AccessTokenOptions;
|
||||
@ -14,18 +14,18 @@ header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
$LIVEKIT_API_KEY = $_ENV['LIVEKIT_API_KEY'] ?? 'devkey';
|
||||
$LIVEKIT_API_SECRET = $_ENV['LIVEKIT_API_SECRET'] ?? 'secret';
|
||||
$LIVEKIT_API_KEY = $_ENV["LIVEKIT_API_KEY"] ?? "devkey";
|
||||
$LIVEKIT_API_SECRET = $_ENV["LIVEKIT_API_SECRET"] ?? "secret";
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['PATH_INFO'] === '/token') {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST" && $_SERVER["PATH_INFO"] === "/token") {
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
$roomName = $data['roomName'] ?? null;
|
||||
$participantName = $data['participantName'] ?? null;
|
||||
$roomName = $data["roomName"] ?? null;
|
||||
$participantName = $data["participantName"] ?? null;
|
||||
|
||||
if (!$roomName || !$participantName) {
|
||||
http_response_code(400);
|
||||
echo "roomName and participantName are required";
|
||||
echo json_encode("roomName and participantName are required");
|
||||
exit();
|
||||
}
|
||||
|
||||
@ -39,9 +39,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['PATH_INFO'] === '/token')
|
||||
->setGrant($videoGrant)
|
||||
->toJwt();
|
||||
|
||||
echo $token;
|
||||
echo json_encode($token);
|
||||
exit();
|
||||
}
|
||||
|
||||
echo "Unsupported endpoint or method";
|
||||
echo json_encode("Unsupported endpoint or method");
|
||||
exit();
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import os
|
||||
from flask import Flask, request
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
from dotenv import load_dotenv
|
||||
from livekit import api
|
||||
@ -20,7 +20,7 @@ def getToken():
|
||||
participant_name = request.json.get("participantName")
|
||||
|
||||
if not room_name or not participant_name:
|
||||
return "roomName and participantName are required", 400
|
||||
return jsonify("roomName and participantName are required"), 400
|
||||
|
||||
token = api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET) \
|
||||
.with_identity(participant_name) \
|
||||
@ -28,7 +28,7 @@ def getToken():
|
||||
room_join=True,
|
||||
room=room_name
|
||||
))
|
||||
return token.to_jwt()
|
||||
return jsonify(token.to_jwt())
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host="0.0.0.0", port=SERVER_PORT)
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
require 'sinatra'
|
||||
require 'sinatra/cors'
|
||||
require 'livekit'
|
||||
require './env.rb'
|
||||
require "sinatra"
|
||||
require "sinatra/cors"
|
||||
require "livekit"
|
||||
require "./env.rb"
|
||||
|
||||
SERVER_PORT = ENV['SERVER_PORT'] || 6080
|
||||
LIVEKIT_API_KEY = ENV['LIVEKIT_API_KEY'] || 'devkey'
|
||||
LIVEKIT_API_SECRET = ENV['LIVEKIT_API_SECRET'] || 'secret'
|
||||
SERVER_PORT = ENV["SERVER_PORT"] || 6080
|
||||
LIVEKIT_API_KEY = ENV["LIVEKIT_API_KEY"] || "devkey"
|
||||
LIVEKIT_API_SECRET = ENV["LIVEKIT_API_SECRET"] || "secret"
|
||||
|
||||
set :port, SERVER_PORT
|
||||
|
||||
@ -14,21 +14,21 @@ set :allow_origin, "*"
|
||||
set :allow_methods, "POST,OPTIONS"
|
||||
set :allow_headers, "content-type"
|
||||
|
||||
post '/token' do
|
||||
post "/token" do
|
||||
content_type :json
|
||||
|
||||
body = JSON.parse(request.body.read)
|
||||
room_name = body['roomName']
|
||||
participant_name = body['participantName']
|
||||
room_name = body["roomName"]
|
||||
participant_name = body["participantName"]
|
||||
|
||||
if room_name.nil? || participant_name.nil?
|
||||
status 400
|
||||
return 'roomName and participantName are required'
|
||||
return JSON.generate("roomName and participantName are required")
|
||||
end
|
||||
|
||||
token = LiveKit::AccessToken.new(api_key: LIVEKIT_API_KEY, api_secret: LIVEKIT_API_SECRET)
|
||||
token.identity = participant_name
|
||||
token.add_grant(roomJoin: true, room: room_name)
|
||||
|
||||
return token.to_jwt
|
||||
return JSON.generate(token.to_jwt)
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user