2019-06-19 06:20:50 +02:00
|
|
|
pub mod default_headers;
|
|
|
|
pub mod errorhandler;
|
2024-09-11 15:51:39 +02:00
|
|
|
pub mod from_fn;
|
2019-06-19 06:20:50 +02:00
|
|
|
pub mod logger;
|
|
|
|
pub mod user_sessions;
|
2019-07-30 19:03:55 +02:00
|
|
|
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
|
|
|
}
|