From bfa586129591af18fc74d21e58e1c21d6ea2cf50 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Tue, 7 Dec 2021 19:23:26 +0800 Subject: [PATCH] update actix-http readme example. --- actix-http/README.md | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/actix-http/README.md b/actix-http/README.md index 92b86d2a3..7b39da312 100644 --- a/actix-http/README.md +++ b/actix-http/README.md @@ -19,35 +19,31 @@ ## Example ```rust -use std::{env, io}; - -use actix_http::{HttpService, Response}; +use actix_http::HttpService; use actix_server::Server; -use futures_util::future; -use http::header::HeaderValue; -use log::info; - -#[actix_rt::main] -async fn main() -> io::Result<()> { - env::set_var("RUST_LOG", "hello_world=info"); - env_logger::init(); +use actix_service::map_config; +use actix_web::{dev::AppConfig, get, App}; +#[actix_web::main] +async fn main() -> std::io::Result<()> { Server::build() .bind("hello-world", "127.0.0.1:8080", || { + // construct actix-web app. + let app = App::new().service(index); 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!")) - }) + // pass the app to service builder. + // map_config is used to map App's configuration to ServiceBuilder. + .finish(map_config(app, |_| AppConfig::default())) .tcp() })? .run() .await } + +#[get("/")] +async fn index() -> &'static str { + "Hello,World from actix-web!" +} ``` ## License