Skip to content

Commit

Permalink
feat: ✨ added request conversion logic for actix web
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Aug 19, 2021
1 parent 02ce425 commit 71a5445
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion examples/showcase/server/src/conv_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ use perseus::{HttpRequest, Request};
// TODO set up proper error handling in an integration crate
/// Converts an Actix Web request into an `http::request`.
pub fn convert_req(raw: &actix_web::HttpRequest) -> Result<Request, String> {
let req = HttpRequest::builder()
let mut builder = HttpRequest::builder();
// Add headers one by one
for (name, val) in raw.headers() {
// Each method call consumes and returns `self`, so we re-self-assign
builder = builder.header(name, val);
}
// The URI to which the request was sent
builder = builder.uri(raw.uri());
// The method (e.g. GET, POST, etc.)
builder = builder.method(raw.method());
// The HTTP version used
builder = builder.version(raw.version());

let req = builder
// We always use an empty body because, in a Perseus request, only the URI matters
// Any custom data should therefore be sent in headers (if you're doing that, consider a dedicated API)
.body(())
.map_err(|err| format!("converting actix web request to perseus-compliant request failed: '{}'", err))?;

Expand Down

0 comments on commit 71a5445

Please sign in to comment.