Rocket/examples/stream/src/main.rs

35 lines
748 B
Rust
Raw Normal View History

#![feature(plugin, decl_macro)]
2016-09-12 09:43:34 +00:00
#![plugin(rocket_codegen)]
extern crate rocket;
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 std::io::{self, repeat, Repeat, Read, Take};
use std::fs::File;
2016-09-12 09:43:34 +00:00
type LimitedRepeat = Take<Repeat>;
2017-05-25 05:45:03 +00:00
// Generate this file using: head -c BYTES /dev/random > big_file.dat
const FILENAME: &'static str = "big_file.dat";
2016-09-12 09:43:34 +00:00
#[get("/")]
fn root() -> content::Plain<Stream<LimitedRepeat>> {
content::Plain(Stream::from(repeat('a' as u8).take(25000)))
2016-09-12 09:43:34 +00:00
}
#[get("/big_file")]
fn file() -> io::Result<Stream<File>> {
File::open(FILENAME).map(|file| Stream::from(file))
}
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() {
2017-05-25 05:45:03 +00:00
rocket().launch();
2016-09-12 09:43:34 +00:00
}