mirror of https://github.com/fafhrd91/actix-web
update actix-http readme example.
This commit is contained in:
parent
606a371ec3
commit
bfa5861295
|
@ -19,35 +19,31 @@
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::{env, io};
|
use actix_http::HttpService;
|
||||||
|
|
||||||
use actix_http::{HttpService, Response};
|
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use futures_util::future;
|
use actix_service::map_config;
|
||||||
use http::header::HeaderValue;
|
use actix_web::{dev::AppConfig, get, App};
|
||||||
use log::info;
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
|
||||||
async fn main() -> io::Result<()> {
|
|
||||||
env::set_var("RUST_LOG", "hello_world=info");
|
|
||||||
env_logger::init();
|
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
Server::build()
|
Server::build()
|
||||||
.bind("hello-world", "127.0.0.1:8080", || {
|
.bind("hello-world", "127.0.0.1:8080", || {
|
||||||
|
// construct actix-web app.
|
||||||
|
let app = App::new().service(index);
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(1000)
|
// pass the app to service builder.
|
||||||
.client_disconnect(1000)
|
// map_config is used to map App's configuration to ServiceBuilder.
|
||||||
.finish(|_req| {
|
.finish(map_config(app, |_| AppConfig::default()))
|
||||||
info!("{:?}", _req);
|
|
||||||
let mut res = Response::Ok();
|
|
||||||
res.header("x-head", HeaderValue::from_static("dummy value!"));
|
|
||||||
future::ok::<_, ()>(res.body("Hello world!"))
|
|
||||||
})
|
|
||||||
.tcp()
|
.tcp()
|
||||||
})?
|
})?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
async fn index() -> &'static str {
|
||||||
|
"Hello,World from actix-web!"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
Loading…
Reference in New Issue