Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example returning HttpResponse from middleware. #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions middleware/middleware-return-httpresponse/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "middleware-return-httpresponse"
version = "1.0.0"
edition = "2021"

[dependencies]
actix-web = "4"
env_logger = "0.9"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
log = "0.4"
pin-project = "1"
serde = { version = "*", features = ["derive"] }
35 changes: 35 additions & 0 deletions middleware/middleware-return-httpresponse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Middleware : Return HttpResponse from Middleware

```rs
cd middleware-return-httpresponse
cargo run
# Started http server: 127.0.0.1:8080
```

## What is this?

A Middleware example which returning HttpResponse.

## How to test

### success case
```sh
curl http://127.0.0.1:8080/ -H 'Authorization:ok' | json_pp -json_opt pretty,canonical
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 42 100 42 0 0 42000 0 --:--:-- --:--:-- --:--:-- 42000
{
"data" : "Hello this is success response!"
}
```

### failed case
```sh
curl http://127.0.0.1:8080/ | json_pp -json_opt pretty,canonical
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 102 100 102 0 0 99k 0 --:--:-- --:--:-- --:--:-- 99k
{
"data" : "Hello this is default error message! you need to set Authorization header to get thru this."
}
```
40 changes: 40 additions & 0 deletions middleware/middleware-return-httpresponse/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use actix_web::{web, App, HttpServer, HttpResponse};

mod simple;

// You can move this struct to a separate file.
// this struct below just for example.
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct HttpData {
pub data: String,
}
// this implementation is optional
impl Default for HttpData {
fn default() -> Self {
Self {
data: "Hello this is success response!".to_string(),
}
}
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

log::info!("starting HTTP server at http://localhost:8080");

HttpServer::new(|| {
App::new()
.wrap(simple::ReturnHttpResponse)
.service(
web::resource("/").to(|| async {
HttpResponse::Ok().json(HttpData::default())
}),
)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
91 changes: 91 additions & 0 deletions middleware/middleware-return-httpresponse/src/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::{
future::{ready, Ready},
rc::Rc
};

use actix_web::{
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
Error,
http::{header, StatusCode}, HttpResponseBuilder
};
use futures_util::future::LocalBoxFuture;


// You can move this struct to a separate file.
// this struct below just for example.
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct HttpData {
pub data: String,
}
// this implementation is optional
impl Default for HttpData {
fn default() -> Self {
Self {
data: "Hello this is default error message! you need to set Authorization header to get thru this.".to_string(),
}
}
}


pub struct ReturnHttpResponse;

impl<S: 'static> Transform<S, ServiceRequest> for ReturnHttpResponse
where
S: Service<ServiceRequest, Response = ServiceResponse, Error = Error>,
S::Future: 'static
{
type Response = ServiceResponse;
type Error = Error;
type InitError = ();
type Transform = AuthMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(AuthMiddleware {
service: Rc::new(service),
}))
}
}

pub struct AuthMiddleware<S> {
// This is special: We need this to avoid lifetime issues.
service: Rc<S>,
}

impl<S> Service<ServiceRequest> for AuthMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
S::Future: 'static
{
type Response = ServiceResponse;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

dev::forward_ready!(service);

fn call(&self, req: ServiceRequest) -> Self::Future {
let svc = self.service.clone();

Box::pin(async move {

let headers = req.headers();
let _ = match headers.get("Authorization") {
Some(e) => e,
None => {
let new_response = HttpResponseBuilder::new(StatusCode::BAD_REQUEST)
.insert_header((header::CONTENT_TYPE, "application/json"))
.json(HttpData::default());
return Ok(ServiceResponse::new(
req.request().to_owned(), /* or req.request().clone() */
new_response
))
}
};

let res = svc.call(req).await?;
Ok(res)
})
}
}