Skip to content

Commit

Permalink
added proxy cache
Browse files Browse the repository at this point in the history
  • Loading branch information
robrotheram committed Feb 25, 2024
1 parent afb9eef commit 2e3a612
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions pkg/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,28 +411,35 @@ func (h *handler) handleMediaProxy(w http.ResponseWriter, r *http.Request) {
http.Error(w, "URL parameter is missing", http.StatusBadRequest)
return
}
// Make an HTTP request to the provided URL
resp, err := http.Get(media.GetAudioUrl())
// Create a new request to the destination URL
req, err := http.NewRequest(r.Method, media.GetAudioUrl(), r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to fetch URL: %s", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Failed to create request: %s", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
req.Header = r.Header

// Forward the response from the external URL back to the client
body, err := io.ReadAll(resp.Body)
// Perform the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to read response body: %s", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Failed to perform request: %s", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()

// Set the content type based on the response header
contentType := resp.Header.Get("Content-Type")
if contentType != "" {
w.Header().Set("Content-Type", contentType)
// Copy headers from the response
for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}

// Write the response body back to the client
w.WriteHeader(resp.StatusCode)
w.Write(body)

// Stream the response body to the client
_, err = io.Copy(w, resp.Body)
if err != nil {
fmt.Printf("Failed to stream response body: %s\n", err)
return
}
}
2 changes: 1 addition & 1 deletion ui/src/pages/app/components/player/VideoPlayer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const VideoPlayer = ({ state, connection }) => {
const getMediaUrl = () => {
switch(state.current.type){
case "YOUTUBE_LIVE": return state.current.url
case "YOUTUBE": return `/api/channel/${getRoomId()}/proxy`
case "YOUTUBE": return `/api/channel/${getRoomId()}/proxy?id=${state.current.id}`
default: return state.current.audio_url
}
}
Expand Down

0 comments on commit 2e3a612

Please sign in to comment.