Change Router::with_state and impl Service for Router<()> (#1552)

* Implement `Service` for `Router<(), B>`

* wip

* wip

* fix some tests

* fix examples

* fix doc tests

* clean up docs

* changelog

* fix

* also call `with_state` when converting `MethodRouter` into a `MakeService`

* suggestions from review
This commit is contained in:
David Pedersen
2022-11-24 14:43:10 +00:00
committed by GitHub
parent fde38f6618
commit 0b26411f39
45 changed files with 576 additions and 738 deletions
+2 -2
View File
@@ -50,7 +50,7 @@ mod tests {
#[tokio::test]
async fn test_get() {
let app = app().into_service();
let app = app();
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().into_service();
let app = app();
let response = app
.oneshot(Request::head("/get-head").body(Body::empty()).unwrap())
+1 -3
View File
@@ -35,9 +35,7 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();
let router_svc = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.into_service();
let router_svc = Router::new().route("/", get(|| async { "Hello, World!" }));
let service = tower::service_fn(move |req: Request<Body>| {
let router_svc = router_svc.clone();
+1 -1
View File
@@ -26,7 +26,7 @@ use std::{
use tower::{BoxError, ServiceBuilder};
use tower_http::{
auth::RequireAuthorizationLayer, compression::CompressionLayer, limit::RequestBodyLimitLayer,
trace::TraceLayer, ServiceBuilderExt,
trace::TraceLayer,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@@ -104,7 +104,6 @@ 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)).into_service();
let rest = Router::new().route("/", get(web_root));
// build the grpc service
let grpc = GreeterServer::new(GrpcServiceImpl::default());
+1 -2
View File
@@ -40,8 +40,7 @@ fn main() {
#[allow(clippy::let_and_return)]
async fn app(request: Request<String>) -> Response {
let mut router = Router::new().route("/api/", get(index)).into_service();
let mut router = Router::new().route("/api/", get(index));
let response = router.call(request).await.unwrap();
response
}
+4 -4
View File
@@ -61,7 +61,7 @@ mod tests {
#[tokio::test]
async fn hello_world() {
let app = app().into_service();
let app = app();
// `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().into_service();
let app = app();
let response = app
.oneshot(
@@ -103,7 +103,7 @@ mod tests {
#[tokio::test]
async fn not_found() {
let app = app().into_service();
let app = app();
let response = app
.oneshot(
@@ -154,7 +154,7 @@ mod tests {
// in multiple request
#[tokio::test]
async fn multiple_request() {
let mut app = app().into_service();
let mut app = app();
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
let response = app.ready().await.unwrap().call(request).await.unwrap();