1
0
mirror of https://github.com/actix/examples synced 2024-12-03 18:22:14 +01:00
examples/middleware/src/simple.rs

26 lines
815 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-16 08:36:53 +02:00
fn start(&self, req: &HttpRequest<S>) -> Result<Started> {
2018-07-09 21:36:03 +02:00
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
2018-07-16 08:36:53 +02:00
fn response(&self, _req: &HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
2018-07-09 21:36:03 +02:00
println!("Hi from response");
Ok(Response::Done(resp))
}
2018-07-16 08:36:53 +02:00
fn finish(&self, _req: &HttpRequest<S>, _resp: &HttpResponse) -> Finished {
2018-07-09 21:36:03 +02:00
println!("Hi from finish");
Finished::Done
}
2018-07-09 20:18:31 +02:00
}