Skip to content

Commit 6202b0c

Browse files
committed
refactor: create inject_deployment middleware
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent edb5f03 commit 6202b0c

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

Cargo.lock

Lines changed: 34 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/service/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tap_core.workspace = true
4040
uuid.workspace = true
4141
alloy.workspace = true
4242
tower_governor = "0.4.0"
43-
tower-http = { version = "0.5.2", features = [
43+
tower-http = { version = "0.6.2", features = [
4444
"auth",
4545
"cors",
4646
"normalize-path",
@@ -57,6 +57,9 @@ bip39.workspace = true
5757
[dev-dependencies]
5858
hex-literal = "0.4.1"
5959
test-assets = { path = "../test-assets" }
60+
tower-test = "0.4.0"
61+
tower = "0.5.1"
62+
tokio-test = "0.4.4"
6063

6164
[build-dependencies]
6265
build-info-build = { version = "0.0.39", default-features = false }

crates/service/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod cli;
55
mod database;
66
mod error;
7+
mod middleware;
78
mod routes;
89
pub mod service;
910
mod tap;

crates/service/src/middleware.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
mod inject_deployment;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use axum::{
5+
extract::{Path, Request},
6+
middleware::Next,
7+
response::Response,
8+
RequestExt,
9+
};
10+
use thegraph_core::DeploymentId;
11+
12+
pub async fn deployment_middleware(mut request: Request, next: Next) -> Response {
13+
let deployment_id = request.extract_parts::<Path<DeploymentId>>().await.ok();
14+
if let Some(Path(deployment_id)) = deployment_id {
15+
request.extensions_mut().insert(deployment_id);
16+
}
17+
next.run(request).await
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::deployment_middleware;
23+
use axum::{
24+
body::Body,
25+
http::{Extensions, Request},
26+
middleware::from_fn,
27+
routing::get,
28+
Router,
29+
};
30+
use reqwest::StatusCode;
31+
use test_assets::ESCROW_SUBGRAPH_DEPLOYMENT;
32+
use thegraph_core::DeploymentId;
33+
use tower::ServiceExt;
34+
35+
#[tokio::test]
36+
async fn test_deployment_middleware() {
37+
let middleware = from_fn(deployment_middleware);
38+
39+
async fn handle(extensions: Extensions) -> Body {
40+
extensions
41+
.get::<DeploymentId>()
42+
.expect("Should contain a deployment_id");
43+
Body::empty()
44+
}
45+
46+
let app = Router::new()
47+
.route("/:deployment_id", get(handle))
48+
.layer(middleware);
49+
50+
let res = app
51+
.oneshot(
52+
Request::builder()
53+
.uri(format!("/{}", *ESCROW_SUBGRAPH_DEPLOYMENT))
54+
.body(Body::empty())
55+
.unwrap(),
56+
)
57+
.await
58+
.unwrap();
59+
assert_eq!(res.status(), StatusCode::OK);
60+
}
61+
}

0 commit comments

Comments
 (0)