add token endpoint for livekit

This commit is contained in:
talksik
2026-04-01 08:22:36 -07:00
parent 47e2a9baa4
commit edc1176e3e
5 changed files with 403 additions and 96 deletions
+59 -13
View File
@@ -11,6 +11,7 @@ import (
"github.com/flowy-live/llink/internal/auth"
"github.com/flowy-live/llink/internal/depot"
"github.com/flowy-live/llink/internal/human"
"github.com/flowy-live/llink/internal/livekit"
"github.com/flowy-live/llink/internal/middleware"
"github.com/flowy-live/llink/internal/network"
"github.com/flowy-live/llink/internal/particle"
@@ -19,22 +20,24 @@ import (
)
type Handler struct {
authSvc auth.AuthService
humanSvc human.Service
networkSvc network.Service
particleSvc particle.Service
depotSvc depot.Service
waitlistSvc waitlist.Service
authSvc auth.AuthService
humanSvc human.Service
networkSvc network.Service
particleSvc particle.Service
depotSvc depot.Service
waitlistSvc waitlist.Service
livekitClient livekit.Client
}
func NewHandler(authSvc auth.AuthService, humanSvc human.Service, networkSvc network.Service, particleSvc particle.Service, depotSvc depot.Service, waitlistSvc waitlist.Service) *Handler {
func NewHandler(authSvc auth.AuthService, humanSvc human.Service, networkSvc network.Service, particleSvc particle.Service, depotSvc depot.Service, waitlistSvc waitlist.Service, livekitClient livekit.Client) *Handler {
return &Handler{
authSvc: authSvc,
humanSvc: humanSvc,
networkSvc: networkSvc,
particleSvc: particleSvc,
depotSvc: depotSvc,
waitlistSvc: waitlistSvc,
authSvc: authSvc,
humanSvc: humanSvc,
networkSvc: networkSvc,
particleSvc: particleSvc,
depotSvc: depotSvc,
waitlistSvc: waitlistSvc,
livekitClient: livekitClient,
}
}
@@ -109,6 +112,16 @@ type RevokeInvitationRequest struct {
Email string `json:"email"`
}
// LiveKit DTOs
type GetLivekitTokenRequest struct {
RoomId string `json:"room_id"`
}
type GetLivekitTokenResponse struct {
Token string `json:"token"`
}
// Depot DTOs
type PrepareUploadRequest struct {
@@ -1040,6 +1053,39 @@ func (h *Handler) networkToDTO(ctx context.Context, n *network.Network) (Network
}, nil
}
// ============================================================================
// LiveKit Handlers
// ============================================================================
func (h *Handler) GetLivekitToken(w http.ResponseWriter, r *http.Request) {
humanId, ok := middleware.HumanIdFromContext(r.Context())
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
var req GetLivekitTokenRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest)
return
}
if req.RoomId == "" {
http.Error(w, "room_id is required", http.StatusBadRequest)
return
}
token, err := h.livekitClient.GetJoinToken(req.RoomId, humanId)
if err != nil {
slog.Error("failed to generate livekit token", "error", err, "humanId", humanId, "roomId", req.RoomId)
http.Error(w, "failed to generate token", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(GetLivekitTokenResponse{Token: token})
}
func extractBearerToken(r *http.Request) string {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {