1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 11:02:08 +01:00

Update README example

This commit is contained in:
Yuki Okushi 2020-03-08 00:43:01 +09:00
parent 10e3e72595
commit cf721c5fff
No known key found for this signature in database
GPG Key ID: B0986C85C0E2DAA1

View File

@ -14,19 +14,34 @@ Actix http
```rust ```rust
// see examples/framed_hello.rs for complete list of used crates. // see examples/framed_hello.rs for complete list of used crates.
extern crate actix_http; use std::{env, io};
use actix_http::{h1, Response, ServiceConfig};
fn main() { use actix_http::{HttpService, Response};
Server::new().bind("framed_hello", "127.0.0.1:8080", || { use actix_server::Server;
IntoFramed::new(|| h1::Codec::new(ServiceConfig::default())) // <- create h1 codec use futures::future;
.and_then(TakeItem::new().map_err(|_| ())) // <- read one request use http::header::HeaderValue;
.and_then(|(_req, _framed): (_, Framed<_, _>)| { // <- send response and close conn use log::info;
SendResponse::send(_framed, Response::Ok().body("Hello world!"))
.map_err(|_| ()) #[actix_rt::main]
.map(|_| ()) async fn main() -> io::Result<()> {
}) env::set_var("RUST_LOG", "hello_world=info");
}).unwrap().run(); env_logger::init();
Server::build()
.bind("hello-world", "127.0.0.1:8080", || {
HttpService::build()
.client_timeout(1000)
.client_disconnect(1000)
.finish(|_req| {
info!("{:?}", _req);
let mut res = Response::Ok();
res.header("x-head", HeaderValue::from_static("dummy value!"));
future::ok::<_, ()>(res.body("Hello world!"))
})
.tcp()
})?
.run()
.await
} }
``` ```