another way to read files using tokio_codec
This commit is contained in:
parent
805aec1572
commit
3c1d7afc42
|
@ -19,6 +19,7 @@ serde_derive = "1.0.80"
|
||||||
url = "1.7.1"
|
url = "1.7.1"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
tokio = "0.1.11"
|
tokio = "0.1.11"
|
||||||
|
tokio-codec = "0.1.1"
|
||||||
http = "0.1.13"
|
http = "0.1.13"
|
||||||
hyper = "0.12.14"
|
hyper = "0.12.14"
|
||||||
lazy_static = "1.1.0"
|
lazy_static = "1.1.0"
|
||||||
|
|
28
src/api3.rs
28
src/api3.rs
|
@ -8,6 +8,10 @@ use serde_json::{json, Value};
|
||||||
|
|
||||||
use futures::future::*;
|
use futures::future::*;
|
||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
|
use tokio::fs::File;
|
||||||
|
use tokio::io;
|
||||||
|
use tokio_codec;
|
||||||
|
|
||||||
use hyper::{Method, Body, Request, Response, Server, StatusCode};
|
use hyper::{Method, Body, Request, Response, Server, StatusCode};
|
||||||
|
|
||||||
fn test_sync_api_handler(param: Value, info: &ApiMethod) -> Result<Value, Error> {
|
fn test_sync_api_handler(param: Value, info: &ApiMethod) -> Result<Value, Error> {
|
||||||
|
@ -37,6 +41,30 @@ fn test_async_api_handler(
|
||||||
let task = lazy(|| {
|
let task = lazy(|| {
|
||||||
println!("A LAZY TASK");
|
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"));
|
let mut resp = Response::new(Body::from("A LAZY TASKs RESPONSE"));
|
||||||
*resp.status_mut() = StatusCode::OK;
|
*resp.status_mut() = StatusCode::OK;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue