1
0
mirror of https://github.com/actix/actix-website synced 2024-11-27 18:12:57 +01:00
actix-website/examples/middleware/src/main.rs

89 lines
2.2 KiB
Rust
Raw Normal View History

pub mod default_headers;
pub mod errorhandler;
pub mod from_fn;
pub mod logger;
pub mod user_sessions;
pub mod wrap_fn;
2019-06-26 08:59:20 +02:00
// <simple>
2022-02-26 04:56:24 +01:00
use std::future::{ready, Ready};
2019-12-28 18:03:17 +01:00
2022-02-26 04:56:24 +01:00
use actix_web::{
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
Error,
};
use futures_util::future::LocalBoxFuture;
2019-06-17 22:57:57 +02:00
// There are two steps in middleware processing.
// 1. Middleware initialization, middleware factory gets called with
// next service in chain as parameter.
// 2. Middleware's call method gets called with normal request.
pub struct SayHi;
2022-02-26 04:56:24 +01:00
// Middleware factory is `Transform` trait
2019-06-17 22:57:57 +02:00
// `S` - type of the next service
// `B` - type of response's body
2022-02-26 04:56:24 +01:00
impl<S, B> Transform<S, ServiceRequest> for SayHi
2019-06-17 22:57:57 +02:00
where
2022-02-26 04:56:24 +01:00
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
2019-06-17 22:57:57 +02:00
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
2019-12-28 18:03:17 +01:00
type Future = Ready<Result<Self::Transform, Self::InitError>>;
2019-06-17 22:57:57 +02:00
fn new_transform(&self, service: S) -> Self::Future {
2022-02-26 04:56:24 +01:00
ready(Ok(SayHiMiddleware { service }))
2019-06-17 22:57:57 +02:00
}
}
pub struct SayHiMiddleware<S> {
service: S,
}
2022-02-26 04:56:24 +01:00
impl<S, B> Service<ServiceRequest> for SayHiMiddleware<S>
2019-06-17 22:57:57 +02:00
where
2022-02-26 04:56:24 +01:00
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
2019-06-17 22:57:57 +02:00
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
2022-02-26 04:56:24 +01:00
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
2019-06-17 22:57:57 +02:00
2022-02-26 04:56:24 +01:00
forward_ready!(service);
2019-06-17 22:57:57 +02:00
2022-02-26 04:56:24 +01:00
fn call(&self, req: ServiceRequest) -> Self::Future {
2019-06-17 22:57:57 +02:00
println!("Hi from start. You requested: {}", req.path());
2019-12-28 18:03:17 +01:00
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
2019-06-17 22:57:57 +02:00
println!("Hi from response");
Ok(res)
2019-12-28 18:03:17 +01:00
})
2019-06-17 22:57:57 +02:00
}
}
2019-06-26 08:59:20 +02:00
// </simple>
2020-09-12 17:21:54 +02:00
#[actix_web::main]
2019-12-28 18:03:17 +01:00
async fn main() -> std::io::Result<()> {
2019-06-26 08:59:20 +02:00
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new().wrap(SayHi).service(
2022-02-26 04:56:24 +01:00
web::resource("/").to(|| async {
"Hello, middleware! Check the console where the server is run."
}),
2019-06-26 08:59:20 +02:00
)
})
2022-02-26 04:56:24 +01:00
.bind(("127.0.0.1", 8080))?
2019-06-26 08:59:20 +02:00
.run()
2019-12-28 18:03:17 +01:00
.await
2019-06-17 22:57:57 +02:00
}