diff --git a/middleware/README.md b/middleware/README.md new file mode 100644 index 00000000..056ce654 --- /dev/null +++ b/middleware/README.md @@ -0,0 +1,11 @@ +## Middleware example + +```bash +cd form +cargo run +# Started http server: 127.0.0.1:8080 +``` + +Look in `src/main.rs` and comment the different middlewares in/out to see how +they function. + diff --git a/middleware/src/main.rs b/middleware/src/main.rs index 18242c6b..539a3076 100644 --- a/middleware/src/main.rs +++ b/middleware/src/main.rs @@ -3,6 +3,9 @@ extern crate actix_web; use actix_web::{server, App}; +#[allow(dead_code)] +mod redirect; +#[allow(dead_code)] mod simple; fn main() { @@ -11,8 +14,14 @@ fn main() { let _addr = server::new(|| { App::new() .middleware(simple::SayHi) - .resource("/index.html", |r| r.f(|_| "Hello, middleware!")) - }).bind("0.0.0.0:8080") + // .middleware(redirect::CheckLogin) + .resource("/login", |r| { + r.f(|_| "You are on /login. Go to src/redirect.rs to change this behavior.") + }) + .resource("/", |r| { + r.f(|_| "Hello, middleware! Check the console where the server is run.") + }) + }).bind("127.0.0.1:8080") .unwrap() .start(); diff --git a/middleware/src/redirect.rs b/middleware/src/redirect.rs new file mode 100644 index 00000000..60f54ca3 --- /dev/null +++ b/middleware/src/redirect.rs @@ -0,0 +1,28 @@ +extern crate actix_web; + +use actix_web::middleware::{Middleware, Started}; +use actix_web::{http, HttpRequest, HttpResponse, Result}; + +pub struct CheckLogin; + +impl Middleware for CheckLogin { + // We only need to hook into the `start` for this middleware. + fn start(&self, req: &mut HttpRequest) -> Result { + let is_logged_in = false; // Change this to see the change in outcome in the browser + + if is_logged_in { + return Ok(Started::Done); + } + + // Don't forward to /login if we are already on /login + if req.path() == "/login" { + return Ok(Started::Done); + } + + Ok(Started::Response( + HttpResponse::Found() + .header(http::header::LOCATION, "/login") + .finish(), + )) + } +} diff --git a/middleware/src/simple.rs b/middleware/src/simple.rs index d1b9f998..e39689a8 100644 --- a/middleware/src/simple.rs +++ b/middleware/src/simple.rs @@ -1,13 +1,29 @@ extern crate actix_web; -use actix_web::middleware::{Middleware, Started}; -use actix_web::{HttpRequest, Result}; +use actix_web::middleware::{Finished, Middleware, Response, Started}; +use actix_web::{HttpRequest, HttpResponse, Result}; +// Middleware can get called at three stages during the request/response handling. Below is a +// struct that implements all three of them. pub struct SayHi; impl Middleware for SayHi { - fn start(&self, _req: &mut HttpRequest) -> Result { - println!("Hi"); + fn start(&self, req: &mut HttpRequest) -> Result { + println!("Hi from start. You requested: {}", req.path()); Ok(Started::Done) } + + fn response( + &self, + _req: &mut HttpRequest, + resp: HttpResponse, + ) -> Result { + println!("Hi from response"); + Ok(Response::Done(resp)) + } + + fn finish(&self, _req: &mut HttpRequest, _resp: &HttpResponse) -> Finished { + println!("Hi from finish"); + Finished::Done + } }