Split RouterService off of Router (#1381)

This commit is contained in:
Jonas Platte
2022-09-22 12:10:55 +02:00
committed by GitHub
parent 18e3fac5d3
commit 69d64cecc3
26 changed files with 371 additions and 225 deletions
+3 -3
View File
@@ -5,7 +5,7 @@
//! ```
use axum::response::{IntoResponse, Response};
use axum::{http, routing::get, Router};
use axum::{http, routing::get, Router, RouterService};
use std::net::SocketAddr;
fn app() -> Router {
@@ -50,7 +50,7 @@ mod tests {
#[tokio::test]
async fn test_get() {
let app = app();
let app = app().into_service();
let response = app
.oneshot(Request::get("/get-head").body(Body::empty()).unwrap())
@@ -66,7 +66,7 @@ mod tests {
#[tokio::test]
async fn test_implicit_head() {
let app = app();
let app = app().into_service();
let response = app
.oneshot(Request::head("/get-head").body(Body::empty()).unwrap())
+5 -3
View File
@@ -35,15 +35,17 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();
let router = Router::new().route("/", get(|| async { "Hello, World!" }));
let router_svc = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.into_service();
let service = tower::service_fn(move |req: Request<Body>| {
let router = router.clone();
let router_svc = router_svc.clone();
async move {
if req.method() == Method::CONNECT {
proxy(req).await
} else {
router.oneshot(req).await.map_err(|err| match err {})
router_svc.oneshot(req).await.map_err(|err| match err {})
}
}
});
+1 -1
View File
@@ -52,7 +52,7 @@ async fn main() {
)
.route("/keys", get(list_keys))
// Nest our admin routes under `/admin`
.nest("/admin", admin_routes(shared_state))
.nest("/admin", admin_routes(shared_state).into_service())
// Add middleware to all routes
.layer(
ServiceBuilder::new()
@@ -104,6 +104,7 @@ mod tests {
async fn send_request_get_body(query: &str) -> String {
let body = app()
.into_service()
.oneshot(
Request::builder()
.uri(format!("/?{}", query))
+1 -1
View File
@@ -55,7 +55,7 @@ async fn main() {
.init();
// build the rest service
let rest = Router::new().route("/", get(web_root));
let rest = Router::new().route("/", get(web_root)).into_service();
// build the grpc service
let grpc = GreeterServer::new(GrpcServiceImpl::default());
+4 -4
View File
@@ -61,7 +61,7 @@ mod tests {
#[tokio::test]
async fn hello_world() {
let app = app();
let app = app().into_service();
// `Router` implements `tower::Service<Request<Body>>` so we can
// call it like any tower service, no need to run an HTTP server.
@@ -78,7 +78,7 @@ mod tests {
#[tokio::test]
async fn json() {
let app = app();
let app = app().into_service();
let response = app
.oneshot(
@@ -103,7 +103,7 @@ mod tests {
#[tokio::test]
async fn not_found() {
let app = app();
let app = app().into_service();
let response = app
.oneshot(
@@ -154,7 +154,7 @@ mod tests {
// in multiple request
#[tokio::test]
async fn multiple_request() {
let mut app = app();
let mut app = app().into_service();
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
let response = app.ready().await.unwrap().call(request).await.unwrap();