pub mod default_headers; pub mod errorhandler; pub mod logger; pub mod user_sessions; pub mod wrap_fn; // use std::pin::Pin; use std::task::{Context, Poll}; use actix_service::{Service, Transform}; use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error}; use futures::future::{ok, Ready}; use futures::Future; // 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 Transform for SayHi where S: Service, Error = Error>, S::Future: 'static, B: 'static, { type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; type InitError = (); type Transform = SayHiMiddleware; type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { ok(SayHiMiddleware { service }) } } pub struct SayHiMiddleware { service: S, } impl Service for SayHiMiddleware where S: Service, Error = Error>, S::Future: 'static, B: 'static, { type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; type Future = Pin>>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.service.poll_ready(cx) } fn call(&mut self, req: ServiceRequest) -> Self::Future { println!("Hi from start. You requested: {}", req.path()); let fut = self.service.call(req); Box::pin(async move { let res = fut.await?; println!("Hi from response"); Ok(res) }) } } // #[actix_web::main] async fn main() -> std::io::Result<()> { use actix_web::{web, App, HttpServer}; HttpServer::new(|| { App::new().wrap(SayHi).service( web::resource("/") .to(|| async { "Hello, middleware! Check the console where the server is run." }), ) }) .bind("127.0.0.1:8080")? .run() .await }