From 89360e255b5c5848740653f1a35db0c351601950 Mon Sep 17 00:00:00 2001 From: andrzej Date: Sun, 15 Dec 2024 17:32:27 +0100 Subject: [PATCH] get cors working --- auth-server/go.mod | 1 + auth-server/go.sum | 2 ++ auth-server/server.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 auth-server/server.go diff --git a/auth-server/go.mod b/auth-server/go.mod index 7d987c8..dc26255 100644 --- a/auth-server/go.mod +++ b/auth-server/go.mod @@ -51,6 +51,7 @@ require ( github.com/pion/webrtc/v4 v4.0.4 // indirect github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect github.com/redis/go-redis/v9 v9.7.0 // indirect + github.com/rs/cors v1.11.1 github.com/stoewer/go-strcase v1.3.0 // indirect github.com/twitchtv/twirp v8.1.3+incompatible // indirect github.com/wlynxg/anet v0.0.5 // indirect diff --git a/auth-server/go.sum b/auth-server/go.sum index a8e6e0c..370d8e7 100644 --- a/auth-server/go.sum +++ b/auth-server/go.sum @@ -100,6 +100,8 @@ github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+ github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E= github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw= +github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= +github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs= github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/auth-server/server.go b/auth-server/server.go new file mode 100644 index 0000000..87dec0a --- /dev/null +++ b/auth-server/server.go @@ -0,0 +1,36 @@ +package main + +// server.go +import ( + "github.com/rs/cors" + "log" + "net/http" + "os" + "time" + + "github.com/livekit/protocol/auth" +) + +func getJoinToken(room, identity string) string { + at := auth.NewAccessToken(os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET")) + grant := &auth.VideoGrant{ + RoomJoin: true, + Room: room, + } + at.SetVideoGrant(grant). + SetIdentity(identity). + SetValidFor(time.Hour) + + token, _ := at.ToJWT() + return token +} + +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/getToken", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(getJoinToken("my-room", "identity"))) + }) + + handler := cors.Default().Handler(mux) + log.Fatal(http.ListenAndServe(":8080", handler)) +}