Rocket/examples/stream/src/main.rs

37 lines
835 B
Rust
Raw Normal View History

#![feature(proc_macro_hygiene)]
2016-09-12 09:43:34 +00:00
#[macro_use] extern crate rocket;
2016-09-12 09:43:34 +00:00
2017-05-25 05:45:03 +00:00
#[cfg(test)] mod tests;
use rocket::response::{content, Stream};
2016-09-12 09:43:34 +00:00
2019-08-27 23:40:23 +00:00
use std::io::repeat;
use async_std::fs::File;
2019-08-27 23:40:23 +00:00
use rocket::AsyncReadExt as _;
//type LimitedRepeat = Take<Repeat>;
type LimitedRepeat = Box<dyn futures::io::AsyncRead + Send + Unpin>;
2016-09-12 09:43:34 +00:00
2017-05-25 05:45:03 +00:00
// Generate this file using: head -c BYTES /dev/random > big_file.dat
2018-07-28 16:58:10 +00:00
const FILENAME: &str = "big_file.dat";
2017-05-25 05:45:03 +00:00
2016-09-12 09:43:34 +00:00
#[get("/")]
fn root() -> content::Plain<Stream<LimitedRepeat>> {
2019-08-27 23:40:23 +00:00
content::Plain(Stream::from(Box::new(repeat('a' as u8).take(25000)) as Box<_>))
2016-09-12 09:43:34 +00:00
}
#[get("/big_file")]
async fn file() -> Option<Stream<File>> {
File::open(FILENAME).await.map(Stream::from).ok()
}
2017-05-25 05:45:03 +00:00
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![root, file])
}
2016-09-12 09:43:34 +00:00
fn main() {
let _ = rocket().launch();
2016-09-12 09:43:34 +00:00
}