1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-14 04:41:15 +00:00
actix-web/actix-http/examples/framed_hello.rs

29 lines
972 B
Rust
Raw Normal View History

use std::{env, io};
2018-12-11 02:08:33 +00:00
use actix_codec::Framed;
2018-11-28 06:12:04 +00:00
use actix_http::{h1, Response, SendResponse, ServiceConfig};
2019-03-11 22:09:42 +00:00
use actix_server::{Io, Server};
use actix_service::{fn_service, NewService};
2018-12-11 02:08:33 +00:00
use actix_utils::framed::IntoFramed;
use actix_utils::stream::TakeItem;
2018-11-24 13:38:17 +00:00
use futures::Future;
2019-03-12 23:55:16 +00:00
use tokio_tcp::TcpStream;
2018-11-24 13:38:17 +00:00
fn main() -> io::Result<()> {
2018-11-24 13:38:17 +00:00
env::set_var("RUST_LOG", "framed_hello=info");
env_logger::init();
2018-12-11 02:08:33 +00:00
Server::build()
2018-11-28 06:12:04 +00:00
.bind("framed_hello", "127.0.0.1:8080", || {
2019-03-12 23:55:16 +00:00
fn_service(|io: Io<TcpStream>| Ok(io.into_parts().0))
2019-03-11 22:09:42 +00:00
.and_then(IntoFramed::new(|| h1::Codec::new(ServiceConfig::default())))
2018-11-28 06:12:04 +00:00
.and_then(TakeItem::new().map_err(|_| ()))
.and_then(|(_req, _framed): (_, Framed<_, _>)| {
SendResponse::send(_framed, Response::Ok().body("Hello world!"))
.map_err(|_| ())
.map(|_| ())
})
})?
.run()
2018-11-24 13:38:17 +00:00
}