Skip to content

Commit 02ab45c

Browse files
authored
Merge pull request #5805 from Turbo87/remove-conduit-router
Remove `conduit-router` and `conduit-middleware` dependencies
2 parents 00e9fd0 + a34f276 commit 02ab45c

File tree

11 files changed

+24
-124
lines changed

11 files changed

+24
-124
lines changed

Cargo.lock

-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ clap = { version = "=4.0.32", features = ["derive", "env", "unicode", "wrap_help
3535

3636
conduit = "=0.10.0"
3737
conduit-axum = { path = "conduit-axum" }
38-
conduit-middleware = "=0.10.0"
39-
conduit-router = "=0.10.0"
4038

4139
cookie = { version = "=0.16.1", features = ["secure"] }
4240
dashmap = { version = "=5.4.0", features = ["raw-api"] }

conduit-axum/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ rust-version = "1.56.0"
1111
[dependencies]
1212
axum = "=0.6.1"
1313
conduit = "=0.10.0"
14-
conduit-router = "=0.10.0"
1514
hyper = { version = "=0.14.23", features = ["server", "stream"] }
1615
http = "=0.2.8"
1716
percent-encoding = "=2.2.0"
@@ -22,7 +21,6 @@ tokio = { version = "=1.23.0", features = ["fs"] }
2221
tokio-stream = "=0.1.11"
2322

2423
[dev-dependencies]
25-
conduit-router = "=0.10.0"
2624
futures-util = "=0.3.25"
2725
hyper = { version = "=0.14.23", features = ["client"] }
2826
tokio = { version = "=1.23.0", features = ["macros", "rt-multi-thread"] }

conduit-axum/examples/server.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![deny(clippy::all)]
22

33
use axum::routing::get;
4-
use conduit::{Body, Handler, RequestExt, ResponseResult};
5-
use conduit_axum::{ConduitAxumHandler, Server};
6-
use conduit_router::RouteBuilder;
4+
use conduit::{Body, RequestExt, ResponseResult};
5+
use conduit_axum::ConduitAxumHandler;
76
use http::{header, Response};
87

98
use std::io;
@@ -14,28 +13,22 @@ async fn main() {
1413
tracing_subscriber::fmt::init();
1514

1615
let router = axum::Router::new()
17-
.route("/axum/", get(wrap(endpoint)))
18-
.route("/axum/panic", get(wrap(panic)))
19-
.route("/axum/error", get(wrap(error)));
16+
.route("/", get(wrap(endpoint)))
17+
.route("/panic", get(wrap(panic)))
18+
.route("/error", get(wrap(error)));
2019

21-
let app = build_conduit_handler();
2220
let addr = ([127, 0, 0, 1], 12345).into();
2321

24-
Server::serve(&addr, router, app).await;
22+
axum::Server::bind(&addr)
23+
.serve(router.into_make_service())
24+
.await
25+
.unwrap()
2526
}
2627

2728
pub fn wrap<H>(handler: H) -> ConduitAxumHandler<H> {
2829
ConduitAxumHandler::wrap(handler)
2930
}
3031

31-
fn build_conduit_handler() -> impl Handler {
32-
let mut router = RouteBuilder::new();
33-
router.get("/", endpoint);
34-
router.get("/panic", panic);
35-
router.get("/error", error);
36-
router
37-
}
38-
3932
fn endpoint(_: &mut dyn RequestExt) -> ResponseResult<http::Error> {
4033
let body = b"Hello world!";
4134

conduit-axum/src/fallback.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use axum::extract::{rejection::PathRejection, Extension, FromRequestParts, Path}
1414
use axum::handler::Handler as AxumHandler;
1515
use axum::response::IntoResponse;
1616
use conduit::{Handler, RequestExt};
17-
use conduit_router::RoutePattern;
1817
use http::header::CONTENT_LENGTH;
1918
use http::StatusCode;
2019
use hyper::{Request, Response};
@@ -88,13 +87,7 @@ where
8887
let mut request = ConduitRequest::new(request);
8988
handler
9089
.call(&mut request)
91-
.map(|mut response| {
92-
if let Some(pattern) = request.mut_extensions().remove::<RoutePattern>() {
93-
response.extensions_mut().insert(pattern);
94-
}
95-
96-
conduit_into_axum(response)
97-
})
90+
.map(conduit_into_axum)
9891
.unwrap_or_else(|e| server_error_response(&*e))
9992
})
10093
.await

conduit-axum/src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
//! Typical usage:
1414
//!
1515
//! ```no_run
16+
//! use axum::routing::get;
1617
//! use conduit::Handler;
17-
//! use conduit_axum::Server;
18+
//! use conduit_axum::ConduitAxumHandler;
1819
//! use tokio::runtime::Runtime;
1920
//!
2021
//! #[tokio::main]
2122
//! async fn main() {
22-
//! let router = axum::Router::new();
23+
//! let router = axum::Router::new()
24+
//! .route("/", get(ConduitAxumHandler::wrap(build_conduit_handler())));
2325
//!
24-
//! let app = build_conduit_handler();
2526
//! let addr = ([127, 0, 0, 1], 12345).into();
26-
//! let server = Server::serve(&addr, router, app);
2727
//!
28-
//! server.await;
28+
//! axum::Server::bind(&addr)
29+
//! .serve( router.into_make_service())
30+
//! .await
31+
//! .unwrap();
2932
//! }
3033
//!
3134
//! fn build_conduit_handler() -> impl Handler {
@@ -48,7 +51,6 @@ mod adaptor;
4851
mod error;
4952
mod fallback;
5053
mod file_stream;
51-
mod server;
5254
#[cfg(test)]
5355
mod tests;
5456
mod tokio_utils;
@@ -58,7 +60,6 @@ pub use fallback::{
5860
conduit_into_axum, CauseField, ConduitAxumHandler, ConduitFallback, ErrorField,
5961
RequestParamsExt,
6062
};
61-
pub use server::Server;
6263
pub use tokio_utils::spawn_blocking;
6364

6465
type AxumResponse = axum::response::Response;

conduit-axum/src/server.rs

-27
This file was deleted.

src/controllers/util.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::prelude::*;
22
use crate::util::errors::{forbidden, internal, AppError, AppResult};
33
use conduit_axum::RequestParamsExt;
4-
use conduit_router::RequestParams;
54

65
/// The Origin header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin)
76
/// is sent with CORS requests and POST requests, and indicates where the request comes from.
@@ -31,10 +30,7 @@ pub trait RequestParamExt<'a> {
3130

3231
impl<'a> RequestParamExt<'a> for &'a (dyn RequestExt + 'a) {
3332
fn param(self, key: &str) -> Option<&'a str> {
34-
return if let Some(params) = self.axum_params() {
35-
params.0.get(key).map(|s| &s[..])
36-
} else {
37-
self.params().find(key)
38-
};
33+
self.axum_params()
34+
.and_then(|params| params.0.get(key).map(|s| &s[..]))
3935
}
4036
}

src/middleware.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod prelude {
22
pub use crate::middleware::log_request::CustomMetadataRequestExt;
33
pub use conduit::{box_error, Body, Handler, RequestExt};
4-
pub use conduit_middleware::{AfterResult, AroundMiddleware, BeforeResult, Middleware};
54
pub use http::{header, Response, StatusCode};
65
}
76

src/middleware/update_metrics.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::app::AppState;
22
use axum::extract::{MatchedPath, State};
33
use axum::middleware::Next;
44
use axum::response::Response;
5-
use conduit_router::RoutePattern;
5+
66
use http::Request;
77
use prometheus::IntGauge;
88
use std::time::Instant;
@@ -24,11 +24,7 @@ pub async fn update_metrics<B>(
2424

2525
let endpoint = match matched_path {
2626
Some(ref matched_path) => matched_path.as_str(),
27-
None => response
28-
.extensions()
29-
.get::<RoutePattern>()
30-
.map(|route_pattern| route_pattern.pattern())
31-
.unwrap_or("<unknown>"),
27+
None => "<unknown>",
3228
};
3329
metrics
3430
.response_times

src/router.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ use axum::routing::{delete, get, post, put};
55
use axum::Router;
66
use conduit::{Handler, HandlerResult, RequestExt};
77
use conduit_axum::{CauseField, ConduitAxumHandler};
8-
use conduit_router::RoutePattern;
98

109
use crate::app::AppState;
1110
use crate::controllers::*;
12-
use crate::middleware::app::{add_app_state_extension, RequestApp};
13-
use crate::util::errors::{not_found, AppError, RouteBlocked};
11+
use crate::middleware::app::add_app_state_extension;
12+
use crate::util::errors::{not_found, AppError};
1413
use crate::util::EndpointResult;
1514
use crate::Env;
1615

@@ -207,22 +206,6 @@ struct C(pub fn(&mut dyn RequestExt) -> EndpointResult);
207206

208207
impl Handler for C {
209208
fn call(&self, req: &mut dyn RequestExt) -> HandlerResult {
210-
if let Some(pattern) = req.extensions().get::<RoutePattern>() {
211-
let pattern = pattern.pattern();
212-
213-
// Configure the Sentry `transaction` field *before* we handle the request,
214-
// but *after* the conduit-router has figured out which handler to use.
215-
let tx_name = format!("{} {}", req.method(), pattern);
216-
sentry::configure_scope(|scope| scope.set_transaction(Some(&tx_name)));
217-
218-
// Allow blocking individual routes by their pattern through the `BLOCKED_ROUTES`
219-
// environment variable. This is not in a middleware because we need access to
220-
// `RoutePattern` before executing the response handler.
221-
if req.app().config.blocked_routes.contains(pattern) {
222-
return Ok(RouteBlocked.response());
223-
}
224-
}
225-
226209
let C(f) = *self;
227210
match f(req) {
228211
Ok(resp) => Ok(resp),

0 commit comments

Comments
 (0)