From 3c1d7afc4297c43f7cc0aef5d20b7845061ab615 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 10 Nov 2018 15:12:45 +0100 Subject: [PATCH] another way to read files using tokio_codec --- Cargo.toml | 1 + src/api3.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index b1f236ac..b3a19ba1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ serde_derive = "1.0.80" url = "1.7.1" futures = "0.1.25" tokio = "0.1.11" +tokio-codec = "0.1.1" http = "0.1.13" hyper = "0.12.14" lazy_static = "1.1.0" diff --git a/src/api3.rs b/src/api3.rs index d397f472..6549011b 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -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 { @@ -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;