Webhook receiver to Go and Java. getToken to createToken

This commit is contained in:
pabloFuente 2024-05-13 22:07:23 +02:00
parent 5b7fc25035
commit 3e1153bd3e
6 changed files with 56 additions and 10 deletions

View File

@ -6,7 +6,7 @@ require (
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/joho/godotenv v1.5.1
github.com/livekit/protocol v1.15.0
github.com/livekit/protocol v1.16.0
)
require (
@ -14,13 +14,18 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/frostbyte73/core v0.0.10 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gammazero/deque v0.2.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
@ -29,9 +34,15 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/puzpuzpuz/xsync/v3 v3.1.0 // indirect
github.com/twitchtv/twirp v8.1.3+incompatible // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.uber.org/zap/exp v0.2.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect

View File

@ -56,6 +56,11 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@ -80,6 +85,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
github.com/livekit/protocol v1.15.0 h1:JAatoWKYdFx3D0U4JBWg25ZlrY+NK26xHabFopS2Jhk=
github.com/livekit/protocol v1.15.0/go.mod h1:pnn0Dv+/0K0OFqKHX6J6SreYO1dZxl6tDuAZ1ns8L/w=
github.com/livekit/protocol v1.16.0 h1:TkUuirvfF1xIfpo5szXqAEEgg7QyML8d0O7+4NQpM7w=
github.com/livekit/protocol v1.16.0/go.mod h1:pnn0Dv+/0K0OFqKHX6J6SreYO1dZxl6tDuAZ1ns8L/w=
github.com/livekit/psrpc v0.5.3-0.20240228172457-3724cb4adbc4 h1:253WtQ2VGVHzIIzW9MUZj7vUDDILESU3zsEbiRdxYF0=
github.com/livekit/psrpc v0.5.3-0.20240228172457-3724cb4adbc4/go.mod h1:CQUBSPfYYAaevg1TNCc6/aYsa8DJH4jSRFdCeSZk5u0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@ -151,6 +158,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

View File

@ -1,12 +1,14 @@
package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/webhook"
)
var (
@ -22,7 +24,7 @@ func getEnv(key, defaultValue string) string {
return defaultValue
}
func getToken(context *gin.Context) {
func createToken(context *gin.Context) {
var body struct {
RoomName string `json:"roomName"`
ParticipantName string `json:"participantName"`
@ -54,9 +56,22 @@ func getToken(context *gin.Context) {
context.JSON(http.StatusOK, token)
}
func receiveWebhook(context *gin.Context) {
authProvider := auth.NewSimpleKeyProvider(
LIVEKIT_API_KEY, LIVEKIT_API_SECRET,
)
event, err := webhook.ReceiveWebhookEvent(context.Request, authProvider)
if err != nil {
fmt.Fprintf(os.Stderr, "error validating webhook event: %v", err)
return
}
fmt.Println("LiveKit Webhook", event)
}
func main() {
router := gin.Default()
router.Use(cors.Default())
router.POST("/token", getToken)
router.POST("/token", createToken)
router.POST("/webhook", receiveWebhook)
router.Run(":" + SERVER_PORT)
}

View File

@ -7,9 +7,14 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import io.livekit.server.*;
import io.livekit.server.AccessToken;
import io.livekit.server.RoomJoin;
import io.livekit.server.RoomName;
import io.livekit.server.WebhookReceiver;
import livekit.LivekitWebhook;
@CrossOrigin(origins = "*")
@RestController
@ -20,13 +25,13 @@ public class Controller {
@Value("${livekit.api.secret}")
private String LIVEKIT_API_SECRET;
/**
* @param params JSON object with roomName and participantName
* @return The JWT token
*/
@PostMapping(value = "/token", produces = "application/json")
public ResponseEntity<String> getToken(@RequestBody Map<String, String> params) {
@PostMapping(value = "/token", consumes = "application/json", produces = "application/json")
public ResponseEntity<String> createToken(@RequestBody Map<String, String> params) {
String roomName = params.get("roomName");
String participantName = params.get("participantName");
@ -42,4 +47,11 @@ public class Controller {
return ResponseEntity.ok("\"" + token.toJwt() + "\"");
}
@PostMapping(value = "/webhook", consumes = "application/webhook+json")
public void receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody RequestBody body) {
WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET);
LivekitWebhook.WebhookEvent event = webhookReceiver.receive(body.toString(), authHeader);
System.out.println("LiveKit Webhook event: " + event.toString());
}
}

View File

@ -16,7 +16,7 @@ CORS(app)
@app.post("/token")
def getToken():
def createToken():
room_name = request.json.get("roomName")
participant_name = request.json.get("participantName")

View File

@ -25,7 +25,7 @@ async fn main() {
.allow_origin(Any)
.allow_headers([CONTENT_TYPE]);
let app = Router::new().route("/token", post(get_token)).layer(cors);
let app = Router::new().route("/token", post(create_token)).layer(cors);
let listener = tokio::net::TcpListener::bind("0.0.0.0:".to_string() + &server_port)
.await
@ -33,7 +33,7 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
async fn get_token(payload: Option<Json<Value>>) -> (StatusCode, Json<String>) {
async fn create_token(payload: Option<Json<Value>>) -> (StatusCode, Json<String>) {
if let Some(payload) = payload {
let livekit_api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
let livekit_api_secret =