classlets-clients/auth-server/server.go

37 lines
725 B
Go

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))
}