From ab7736c79095d33901d80b4a59da61a191dbf1ba Mon Sep 17 00:00:00 2001 From: Gorm Casper Date: Mon, 9 Jul 2018 20:18:31 +0200 Subject: [PATCH 1/2] init --- .travis.yml | 1 + Cargo.lock | 8 ++++++++ Cargo.toml | 1 + middleware/Cargo.toml | 8 ++++++++ middleware/src/main.rs | 20 ++++++++++++++++++++ middleware/src/simple.rs | 13 +++++++++++++ 6 files changed, 51 insertions(+) create mode 100644 middleware/Cargo.toml create mode 100644 middleware/src/main.rs create mode 100644 middleware/src/simple.rs diff --git a/.travis.yml b/.travis.yml index 7dff60b..84057bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,6 +45,7 @@ script: cd http-proxy && cargo check && cd .. cd json && cargo check && cd .. cd juniper && cargo check && cd .. + cd middleware && cargo check && cd .. cd multipart && cargo check && cd .. cd protobuf && cargo check && cd .. cd r2d2 && cargo check && cd .. diff --git a/Cargo.lock b/Cargo.lock index df4c8d0..769e118 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1038,6 +1038,14 @@ name = "memoffset" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "middleware-example" +version = "0.1.0" +dependencies = [ + "actix 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.3.7" diff --git a/Cargo.toml b/Cargo.toml index 76331ac..c78478c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "http-proxy", "json", "juniper", + "middleware", "multipart", "protobuf", "r2d2", diff --git a/middleware/Cargo.toml b/middleware/Cargo.toml new file mode 100644 index 0000000..9db8f0b --- /dev/null +++ b/middleware/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "middleware-example" +version = "0.1.0" +authors = ["Gorm Casper "] + +[dependencies] +actix = "0.5" +actix-web = "^0.6" diff --git a/middleware/src/main.rs b/middleware/src/main.rs new file mode 100644 index 0000000..18242c6 --- /dev/null +++ b/middleware/src/main.rs @@ -0,0 +1,20 @@ +extern crate actix; +extern crate actix_web; + +use actix_web::{server, App}; + +mod simple; + +fn main() { + let sys = actix::System::new("middleware-example"); + + let _addr = server::new(|| { + App::new() + .middleware(simple::SayHi) + .resource("/index.html", |r| r.f(|_| "Hello, middleware!")) + }).bind("0.0.0.0:8080") + .unwrap() + .start(); + + let _ = sys.run(); +} diff --git a/middleware/src/simple.rs b/middleware/src/simple.rs new file mode 100644 index 0000000..d1b9f99 --- /dev/null +++ b/middleware/src/simple.rs @@ -0,0 +1,13 @@ +extern crate actix_web; + +use actix_web::middleware::{Middleware, Started}; +use actix_web::{HttpRequest, Result}; + +pub struct SayHi; + +impl Middleware for SayHi { + fn start(&self, _req: &mut HttpRequest) -> Result { + println!("Hi"); + Ok(Started::Done) + } +} From 3b919f58f95a64bcbe6fd416dc154259c03ab510 Mon Sep 17 00:00:00 2001 From: Gorm Casper Date: Mon, 9 Jul 2018 21:36:03 +0200 Subject: [PATCH 2/2] middleware example --- middleware/README.md | 11 +++++++++++ middleware/src/main.rs | 13 +++++++++++-- middleware/src/redirect.rs | 28 ++++++++++++++++++++++++++++ middleware/src/simple.rs | 24 ++++++++++++++++++++---- 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 middleware/README.md create mode 100644 middleware/src/redirect.rs diff --git a/middleware/README.md b/middleware/README.md new file mode 100644 index 0000000..056ce65 --- /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 18242c6..539a307 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 0000000..60f54ca --- /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 d1b9f99..e39689a 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 + } }