mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Merge pull request #27 from casperin/middleware-example
Middleware example
This commit is contained in:
commit
0b52c7850e
@ -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 ..
|
||||
|
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -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"
|
||||
|
@ -12,6 +12,7 @@ members = [
|
||||
"http-proxy",
|
||||
"json",
|
||||
"juniper",
|
||||
"middleware",
|
||||
"multipart",
|
||||
"protobuf",
|
||||
"r2d2",
|
||||
|
8
middleware/Cargo.toml
Normal file
8
middleware/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "middleware-example"
|
||||
version = "0.1.0"
|
||||
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
actix = "0.5"
|
||||
actix-web = "^0.6"
|
11
middleware/README.md
Normal file
11
middleware/README.md
Normal file
@ -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.
|
||||
|
29
middleware/src/main.rs
Normal file
29
middleware/src/main.rs
Normal file
@ -0,0 +1,29 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
|
||||
use actix_web::{server, App};
|
||||
|
||||
#[allow(dead_code)]
|
||||
mod redirect;
|
||||
#[allow(dead_code)]
|
||||
mod simple;
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("middleware-example");
|
||||
|
||||
let _addr = server::new(|| {
|
||||
App::new()
|
||||
.middleware(simple::SayHi)
|
||||
// .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();
|
||||
|
||||
let _ = sys.run();
|
||||
}
|
28
middleware/src/redirect.rs
Normal file
28
middleware/src/redirect.rs
Normal file
@ -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<S> Middleware<S> for CheckLogin {
|
||||
// We only need to hook into the `start` for this middleware.
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
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(),
|
||||
))
|
||||
}
|
||||
}
|
29
middleware/src/simple.rs
Normal file
29
middleware/src/simple.rs
Normal file
@ -0,0 +1,29 @@
|
||||
extern crate actix_web;
|
||||
|
||||
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<S> Middleware<S> for SayHi {
|
||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||
println!("Hi from start. You requested: {}", req.path());
|
||||
Ok(Started::Done)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user