1
0
mirror of https://github.com/actix/examples synced 2024-12-04 10:41:56 +01:00
examples/middleware/src/simple.rs

30 lines
858 B
Rust
Raw Normal View History

2018-07-09 20:18:31 +02:00
extern crate actix_web;
2018-07-09 21:36:03 +02:00
use actix_web::middleware::{Finished, Middleware, Response, Started};
use actix_web::{HttpRequest, HttpResponse, Result};
2018-07-09 20:18:31 +02:00
2018-07-09 21:36:03 +02:00
// Middleware can get called at three stages during the request/response handling. Below is a
// struct that implements all three of them.
2018-07-09 20:18:31 +02:00
pub struct SayHi;
impl<S> Middleware<S> for SayHi {
2018-07-09 21:36:03 +02:00
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
println!("Hi from start. You requested: {}", req.path());
2018-07-09 20:18:31 +02:00
Ok(Started::Done)
}
2018-07-09 21:36:03 +02:00
fn response(
&self,
_req: &mut HttpRequest<S>,
resp: HttpResponse,
) -> Result<Response> {
println!("Hi from response");
Ok(Response::Done(resp))
}
fn finish(&self, _req: &mut HttpRequest<S>, _resp: &HttpResponse) -> Finished {
println!("Hi from finish");
Finished::Done
}
2018-07-09 20:18:31 +02:00
}