2019-06-19 06:20:50 +02:00
|
|
|
pub mod default_headers;
|
|
|
|
pub mod errorhandler;
|
|
|
|
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>
|
2019-12-28 18:03:17 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-06-17 22:57:57 +02:00
|
|
|
use actix_service::{Service, Transform};
|
2019-06-26 08:59:20 +02:00
|
|
|
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
2019-12-28 18:03:17 +01:00
|
|
|
use futures::future::{ok, Ready};
|
|
|
|
use futures::Future;
|
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;
|
|
|
|
|
|
|
|
// Middleware factory is `Transform` trait from actix-service crate
|
|
|
|
// `S` - type of the next service
|
|
|
|
// `B` - type of response's body
|
|
|
|
impl<S, B> Transform<S> for SayHi
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
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 {
|
|
|
|
ok(SayHiMiddleware { service })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SayHiMiddleware<S> {
|
|
|
|
service: S,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service for SayHiMiddleware<S>
|
|
|
|
where
|
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
2019-12-28 18:03:17 +01:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
2019-06-17 22:57:57 +02:00
|
|
|
|
2019-12-28 18:03:17 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(cx)
|
2019-06-17 22:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
|
|
|
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(
|
|
|
|
web::resource("/")
|
2019-12-28 18:03:17 +01:00
|
|
|
.to(|| async {
|
|
|
|
"Hello, middleware! Check the console where the server is run."
|
|
|
|
}),
|
2019-06-26 08:59:20 +02:00
|
|
|
)
|
|
|
|
})
|
2020-09-12 17:21:54 +02: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
|
|
|
}
|