Rocket/examples/stream/src/main.rs

28 lines
663 B
Rust
Raw Normal View History

2016-09-12 09:43:34 +00:00
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
use rocket::response::{data, 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>;
#[get("/")]
fn root() -> data::Plain<Stream<LimitedRepeat>> {
data::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>> {
// Generate this file using: head -c BYTES /dev/random > big_file.dat
const FILENAME: &'static str = "big_file.dat";
File::open(FILENAME).map(|file| Stream::from(file))
}
2016-09-12 09:43:34 +00:00
fn main() {
rocket::ignite().mount_and_launch("/", routes![root, file]);
2016-09-12 09:43:34 +00:00
}