another way to read files using tokio_codec

This commit is contained in:
Dietmar Maurer
2018-11-10 15:12:45 +01:00
parent 805aec1572
commit 3c1d7afc42
2 changed files with 29 additions and 0 deletions

View File

@ -8,6 +8,10 @@ use serde_json::{json, Value};
use futures::future::*;
use tokio::prelude::*;
use tokio::fs::File;
use tokio::io;
use tokio_codec;
use hyper::{Method, Body, Request, Response, Server, StatusCode};
fn test_sync_api_handler(param: Value, info: &ApiMethod) -> Result<Value, Error> {
@ -37,6 +41,30 @@ fn test_async_api_handler(
let task = lazy(|| {
println!("A LAZY TASK");
let dump_file = File::open("/etc/network/interfaces")
.and_then(|file| {
let file = std::io::BufReader::new(file);
let mut linenr = 1;
tokio::io::lines(file).for_each(move |line| {
println!("LINE {}: {}", linenr, line);
linenr += 1;
ok(())
})
}).map_err(|err| ());
//tokio::spawn(dump_file);
let dump_file2 = File::open("/etc/network/interfaces")
.and_then(|file| {
tokio_codec::FramedRead::new(file, tokio_codec::BytesCodec::new()).for_each(|data| {
println!("DATA {:?}", data);
ok(())
})
}).map_err(|err| ()); // fixme: log error
tokio::spawn(dump_file2);
let mut resp = Response::new(Body::from("A LAZY TASKs RESPONSE"));
*resp.status_mut() = StatusCode::OK;