Rocket/examples/stream/src/main.rs

32 lines
702 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
use tokio::fs::File;
use tokio::io::{repeat, AsyncRead, AsyncReadExt};
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<impl AsyncRead>> {
content::Plain(Stream::from(repeat('a' as u8).take(25000)))
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
}