Created
December 12, 2017 14:49
-
-
Save gfriloux/c8846afed6d2bfceffd13fc5375bced3 to your computer and use it in GitHub Desktop.
Streaming data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "chunked" | |
version = "0.1.0" | |
authors = ["Guillaume Friloux <[email protected]>"] | |
[dependencies] | |
rocket = "0.3.3" | |
rocket_codegen = "0.3.3" | |
rocket_contrib = "0.3.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![plugin(rocket_codegen)] | |
extern crate rocket; | |
use std::io; | |
use std::io::Read; | |
use std::fs::File; | |
use rocket::response::Stream; | |
struct PipeLoop | |
{ | |
filename: String | |
} | |
impl PipeLoop { | |
fn new(filename: &str) -> PipeLoop { | |
let p = PipeLoop { | |
filename: filename.to_string() | |
}; | |
p | |
} | |
} | |
impl Read for PipeLoop { | |
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | |
let mut fd; | |
let len; | |
println!("Opening file {}", &self.filename); | |
fd = match File::open(&self.filename) { | |
Err(e) => { | |
println!("Failed to open {} : {}", &self.filename, e); | |
return Ok(0); | |
} | |
Ok(e) => { e } | |
}; | |
println!("Reading file {}", &self.filename); | |
len = match fd.read(buf) { | |
Err(e) => { | |
println!("Failed to read {} : {}", &self.filename, e); | |
return Ok(0); | |
} | |
Ok(e) => { e } | |
}; | |
println!("We successfully read {} bytes", len); | |
Ok(len) | |
} | |
} | |
#[get("/pending")] | |
fn pending() -> Stream<PipeLoop> { | |
let p = PipeLoop::new("/tmp/testfifo"); | |
Stream::from(p) | |
} | |
fn main() { | |
rocket::ignite().mount("/", routes![pending]) | |
.launch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment