mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
middleware example
This commit is contained in:
parent
ab7736c790
commit
3b919f58f9
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.
|
||||||
|
|
@ -3,6 +3,9 @@ extern crate actix_web;
|
|||||||
|
|
||||||
use actix_web::{server, App};
|
use actix_web::{server, App};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
mod redirect;
|
||||||
|
#[allow(dead_code)]
|
||||||
mod simple;
|
mod simple;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -11,8 +14,14 @@ fn main() {
|
|||||||
let _addr = server::new(|| {
|
let _addr = server::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(simple::SayHi)
|
.middleware(simple::SayHi)
|
||||||
.resource("/index.html", |r| r.f(|_| "Hello, middleware!"))
|
// .middleware(redirect::CheckLogin)
|
||||||
}).bind("0.0.0.0:8080")
|
.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()
|
.unwrap()
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,29 @@
|
|||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
|
|
||||||
use actix_web::middleware::{Middleware, Started};
|
use actix_web::middleware::{Finished, Middleware, Response, Started};
|
||||||
use actix_web::{HttpRequest, Result};
|
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;
|
pub struct SayHi;
|
||||||
|
|
||||||
impl<S> Middleware<S> for SayHi {
|
impl<S> Middleware<S> for SayHi {
|
||||||
fn start(&self, _req: &mut HttpRequest<S>) -> Result<Started> {
|
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||||
println!("Hi");
|
println!("Hi from start. You requested: {}", req.path());
|
||||||
Ok(Started::Done)
|
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