mirror of
https://github.com/actix/examples
synced 2025-01-22 22:05:57 +01:00
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
use std::time::Duration;
|
|
|
|
use actix_http::body::MessageBody;
|
|
use actix_web::{dev, rt::time, web, App, Error, HttpServer};
|
|
use actix_web_lab::middleware::{from_fn, Next};
|
|
|
|
mod read_request_body;
|
|
mod read_response_body;
|
|
mod redirect;
|
|
mod simple;
|
|
|
|
// See more examples of from_fn middleware here:
|
|
// https://github.com/robjtede/actix-web-lab/blob/main/actix-web-lab/examples/from_fn.rs
|
|
async fn timeout_10secs(
|
|
req: dev::ServiceRequest,
|
|
next: Next<impl MessageBody + 'static>,
|
|
) -> Result<dev::ServiceResponse<impl MessageBody>, Error> {
|
|
match time::timeout(Duration::from_secs(10), next.call(req)).await {
|
|
Ok(res) => res,
|
|
Err(_err) => Err(actix_web::error::ErrorRequestTimeout("")),
|
|
}
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
|
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
.wrap(redirect::CheckLogin)
|
|
.wrap(read_request_body::Logging)
|
|
.wrap(read_response_body::Logging)
|
|
.wrap(simple::SayHi)
|
|
.wrap(from_fn(timeout_10secs))
|
|
.service(web::resource("/login").to(|body: String| async move {
|
|
println!("request body (handler): {body}");
|
|
"You are on /login. Go to src/redirect.rs to change this behavior."
|
|
}))
|
|
.service(
|
|
web::resource("/").to(|| async {
|
|
"Hello, middleware! Check the console where the server is run."
|
|
}),
|
|
)
|
|
})
|
|
.bind(("127.0.0.1", 8080))?
|
|
.workers(1)
|
|
.run()
|
|
.await
|
|
}
|