30 linhas
879 B
Rust
30 linhas
879 B
Rust
use bytes::Bytes;
|
|
use rocket::response::stream::ByteStream;
|
|
|
|
/// Copy-pasted and modified from https://github.com/rwf2/Rocket/issues/1521
|
|
mod req {
|
|
use super::*;
|
|
use rocket::response::Debug;
|
|
|
|
pub type Result<T, E = Debug<reqwest::Error>> = std::result::Result<T, E>;
|
|
|
|
pub async fn get(url: &str) -> Result<ByteStream![Bytes]> {
|
|
let bytes_stream = reqwest::get(url).await?.bytes_stream();
|
|
Ok(ByteStream! {
|
|
for await bytes in bytes_stream {
|
|
match bytes {
|
|
Ok(bytes) => yield bytes,
|
|
Err(e) => {
|
|
eprintln!("error while streaming: {}", e);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
#[get("/proxy/<url>")]
|
|
pub async fn proxy(url: &str) -> req::Result<ByteStream![Bytes]> {
|
|
req::get(url).await
|
|
}
|