A decentralized music tracking and discovery platform built on AT Protocol 馃幍 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz
at main 90 lines 2.8 kB view raw
1const PORT = parseInt(Deno.env.get("PORT") || "8081"); 2 3console.log(`HTTP Proxy Server is running on http://localhost:${PORT}`); 4 5Deno.serve({ port: PORT }, async (req) => { 6 const url = new URL(req.url); 7 8 try { 9 let target: URL; 10 11 if ( 12 url.pathname.startsWith("/xrpc") || 13 url.pathname.startsWith("/login") || 14 url.pathname.startsWith("/oauth/callback") || 15 url.pathname.startsWith("/users") || 16 url.pathname.startsWith("/albums") || 17 url.pathname.startsWith("/artists") || 18 url.pathname.startsWith("/tracks") || 19 url.pathname.startsWith("/scrobbles") || 20 url.pathname.startsWith("/likes") || 21 url.pathname.startsWith("/spotify") || 22 url.pathname.startsWith("/dropbox/oauth/callback") || 23 url.pathname.startsWith("/googledrive/oauth/callback") || 24 url.pathname.startsWith("/dropbox/files") || 25 url.pathname.startsWith("/dropbox/file") || 26 url.pathname.startsWith("/googledrive/files") || 27 url.pathname.startsWith("/dropbox/login") || 28 url.pathname.startsWith("/googledrive/login") || 29 url.pathname.startsWith("/dropbox/join") || 30 url.pathname.startsWith("/googledrive/join") || 31 url.pathname.startsWith("/search") || 32 url.pathname.startsWith("/public/scrobbles") 33 ) { 34 // API requests 35 target = new URL(url); 36 target.host = "localhost"; 37 target.port = "4004"; 38 } else { 39 // Vite frontend requests 40 target = new URL(url); 41 target.host = "localhost"; 42 target.port = "5174"; 43 } 44 45 // Handle WebSocket connections 46 if (req.headers.get("upgrade") === "websocket") { 47 const { socket, response } = Deno.upgradeWebSocket(req); 48 const wsUrl = `ws://${target.host}${target.pathname}${target.search}`; 49 const ws = new WebSocket(wsUrl); 50 51 ws.onopen = () => { 52 console.log("WebSocket connection established"); 53 }; 54 ws.onmessage = (event) => { 55 socket.send(event.data); 56 }; 57 ws.onclose = () => { 58 socket.close(); 59 }; 60 ws.onerror = (event) => { 61 console.error("WebSocket error:", event); 62 socket.close(); 63 }; 64 socket.onmessage = (event) => { 65 ws.send(event.data); 66 }; 67 socket.onclose = () => { 68 ws.close(); 69 }; 70 return response; 71 } 72 73 // Proxy HTTP requests 74 const proxyRequest = new Request(target.toString(), { 75 method: req.method, 76 headers: req.headers, 77 body: req.body, 78 }); 79 80 const response = await fetch(proxyRequest); 81 82 return new Response(response.body, { 83 status: response.status, 84 headers: response.headers, 85 }); 86 } catch (error) { 87 console.error("Proxy error:", error); 88 return new Response("Failed to fetch the target URL", { status: 500 }); 89 } 90});