Rocket/examples/stream/src/main.rs

37 lines
827 B
Rust

#![feature(proc_macro_hygiene)]
#[macro_use] extern crate rocket;
#[cfg(test)] mod tests;
use rocket::response::{content, Stream};
use std::io::repeat;
use async_std::fs::File;
use rocket::AsyncReadExt as _;
//type LimitedRepeat = Take<Repeat>;
type LimitedRepeat = Box<dyn futures::io::AsyncRead + Send + Unpin>;
// Generate this file using: head -c BYTES /dev/random > big_file.dat
const FILENAME: &str = "big_file.dat";
#[get("/")]
fn root() -> content::Plain<Stream<LimitedRepeat>> {
content::Plain(Stream::from(Box::new(repeat('a' as u8).take(25000)) as Box<_>))
}
#[get("/big_file")]
async fn file() -> Option<Stream<File>> {
File::open(FILENAME).await.map(Stream::from).ok()
}
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![root, file])
}
fn main() {
rocket().launch();
}