classlets-clients/auth-server/server.go

39 lines
838 B
Go

package main
// server.go
import (
"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()
log.Println(token)
return token
}
func main() {
http.HandleFunc("/getToken", func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json")
//HACK: allowing access from any domain would be very bad in production!
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte(getJoinToken("my-room", "identity")))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}