forked from LukeMathWalker/pavex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
ConnectionInfo
extractor (LukeMathWalker#169)
The logic of handlers and middleware often use information concerning the underlying connection. We enable this by adding a `ConnectionInfo` structure to the framework. --------- Co-authored-by: Luca Palmieri <[email protected]>
- Loading branch information
1 parent
397dc04
commit 58452eb
Showing
101 changed files
with
1,203 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
doc_examples/guide/request_data/connection/project-connection_peer.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
```rust title="src/connection/peer.rs" | ||
use pavex::connection::ConnectionInfo; | ||
use pavex::response::Response; | ||
pub fn handler(conn: ConnectionInfo) -> Response { | ||
let addr = conn.peer_addr(); | ||
Response::ok().set_typed_body(format!("Your address is {addr}")) | ||
} | ||
``` |
2 changes: 2 additions & 0 deletions
2
doc_examples/guide/request_data/connection/project/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
13 changes: 13 additions & 0 deletions
13
doc_examples/guide/request_data/connection/project/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "connection" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
pavex = { path = "../../../../../libs/pavex" } | ||
pavex_cli_client = { path = "../../../../../libs/pavex_cli_client" } | ||
serde = { version = "1", features = ["derive"] } | ||
cargo_px_env = "0.1" | ||
|
||
[workspace] | ||
members = [".", "server_sdk"] |
8 changes: 8 additions & 0 deletions
8
doc_examples/guide/request_data/connection/project/server_sdk/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "connection_server_sdk" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[package.metadata.px.generate] | ||
generator_type = "cargo_workspace_binary" | ||
generator_name = "connection" |
1 change: 1 addition & 0 deletions
1
doc_examples/guide/request_data/connection/project/server_sdk/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
8 changes: 8 additions & 0 deletions
8
doc_examples/guide/request_data/connection/project/src/blueprint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use pavex::blueprint::Blueprint; | ||
|
||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
|
||
bp.nest(crate::connection::blueprint()); | ||
bp | ||
} |
9 changes: 9 additions & 0 deletions
9
doc_examples/guide/request_data/connection/project/src/connection/blueprint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use pavex::blueprint::router::GET; | ||
use pavex::blueprint::Blueprint; | ||
use pavex::f; | ||
|
||
pub fn blueprint() -> Blueprint { | ||
let mut bp = Blueprint::new(); | ||
bp.route(GET, "/connection", f!(super::handler)); | ||
bp | ||
} |
5 changes: 5 additions & 0 deletions
5
doc_examples/guide/request_data/connection/project/src/connection/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub use blueprint::blueprint; | ||
pub use peer::handler; | ||
|
||
mod blueprint; | ||
mod peer; |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/request_data/connection/project/src/connection/peer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use pavex::connection::ConnectionInfo; | ||
use pavex::response::Response; | ||
|
||
pub fn handler(conn: ConnectionInfo) -> Response { | ||
let addr = conn.peer_addr(); | ||
Response::ok().set_typed_body(format!("Your address is {addr}")) | ||
} |
7 changes: 7 additions & 0 deletions
7
doc_examples/guide/request_data/connection/project/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
pub use blueprint::blueprint; | ||
|
||
mod blueprint; | ||
pub mod connection; |
13 changes: 13 additions & 0 deletions
13
doc_examples/guide/request_data/connection/project/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::error::Error; | ||
|
||
use cargo_px_env::generated_pkg_manifest_path; | ||
use connection::blueprint; | ||
use pavex_cli_client::Client; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let generated_dir = generated_pkg_manifest_path()?.parent().unwrap().into(); | ||
Client::new() | ||
.generate(blueprint(), generated_dir) | ||
.execute()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
starter_project_folder: "project" | ||
commands: | ||
- command: "cargo px c" | ||
expected_outcome: "success" | ||
snippets: | ||
- name: "connection_peer" | ||
source_path: "src/connection/peer.rs" | ||
ranges: [ ".." ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Connection info | ||
|
||
[`ConnectionInfo`][ConnectionInfo] groups together information about the HTTP connection | ||
used to transmit the request you're currently processing. | ||
|
||
[`ConnectionInfo`][ConnectionInfo] gives you access to the peer address of the client that sent the request, via | ||
the [`ConnectionInfo::peer_addr`][ConnectionInfo::peer_addr] method. | ||
Many applications include the peer address in their request logs, for example. | ||
|
||
!!! warning "Security implications" | ||
|
||
The peer address should **not** be treated as the clients IP | ||
address. Check out ["The perils of the 'real' client IP"](https://adam-p.ca/blog/2022/03/x-forwarded-for/) | ||
by Adam Pritchard to learn more about the issues you might run into. | ||
|
||
## Injection | ||
|
||
Inject [`ConnectionInfo`][ConnectionInfo] to access the peer address via its [`peer_addr`][ConnectionInfo::peer_addr] | ||
method: | ||
|
||
--8<-- "doc_examples/guide/request_data/connection/project-connection_peer.snap" | ||
|
||
[ConnectionInfo]: ../../../api_reference/pavex/connection/struct.ConnectionInfo.html | ||
[ConnectionInfo::peer_addr]: ../../../api_reference/pavex/connection/struct.ConnectionInfo.html#method.peer_addr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
server: | ||
port: 8000 | ||
port: 8000 | ||
app: | ||
cookie: | ||
percent_encode: true | ||
crypto_rules: [ ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.