diff --git a/examples/async-graphql/src/main.rs b/examples/async-graphql/src/main.rs index 7d03d3b9..1c2b1211 100644 --- a/examples/async-graphql/src/main.rs +++ b/examples/async-graphql/src/main.rs @@ -34,7 +34,7 @@ async fn main() { .data(StarWars::new()) .finish(); - let app = Router::new() + let app = Router::without_state() .route("/", get(graphql_playground).post(graphql_handler)) .layer(Extension(schema)); diff --git a/examples/chat/src/main.rs b/examples/chat/src/main.rs index 5107323e..3eae69c5 100644 --- a/examples/chat/src/main.rs +++ b/examples/chat/src/main.rs @@ -44,7 +44,7 @@ async fn main() { let app_state = Arc::new(AppState { user_set, tx }); - let app = Router::new() + let app = Router::without_state() .route("/", get(index)) .route("/websocket", get(websocket_handler)) .layer(Extension(app_state)); diff --git a/examples/consume-body-in-extractor-or-middleware/src/main.rs b/examples/consume-body-in-extractor-or-middleware/src/main.rs index 1fdd9022..a7efd1ce 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -29,7 +29,7 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let app = Router::new().route("/", post(handler)).layer( + let app = Router::without_state().route("/", post(handler)).layer( ServiceBuilder::new() .map_request_body(body::boxed) .layer(middleware::from_fn(print_request_body)), @@ -80,17 +80,22 @@ async fn handler(_: PrintRequestBody, body: Bytes) { struct PrintRequestBody; #[async_trait] -impl FromRequest for PrintRequestBody { +impl FromRequest for PrintRequestBody +where + S: Clone + Send + Sync + 'static, +{ type Rejection = Response; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { + let state = req.state().clone(); + let request = Request::from_request(req) .await .map_err(|err| err.into_response())?; let request = buffer_request_body(request).await?; - *req = RequestParts::new(request); + *req = RequestParts::new(state, request); Ok(Self) } diff --git a/examples/cors/src/main.rs b/examples/cors/src/main.rs index a8e02cc5..cb0982ee 100644 --- a/examples/cors/src/main.rs +++ b/examples/cors/src/main.rs @@ -7,7 +7,7 @@ use axum::{ http::{HeaderValue, Method}, response::{Html, IntoResponse}, - routing::get, + routing::{get, WithState}, Json, Router, }; use std::net::SocketAddr; @@ -16,12 +16,12 @@ use tower_http::cors::CorsLayer; #[tokio::main] async fn main() { let frontend = async { - let app = Router::new().route("/", get(html)); + let app = Router::without_state().route("/", get(html)); serve(app, 3000).await; }; let backend = async { - let app = Router::new().route("/json", get(json)).layer( + let app = Router::without_state().route("/json", get(json)).layer( // see https://docs.rs/tower-http/latest/tower_http/cors/index.html // for more details // @@ -38,7 +38,7 @@ async fn main() { tokio::join!(frontend, backend); } -async fn serve(app: Router, port: u16) { +async fn serve(app: Router<(), WithState>, port: u16) { let addr = SocketAddr::from(([127, 0, 0, 1], port)); axum::Server::bind(&addr) .serve(app.into_make_service()) diff --git a/examples/customize-extractor-error/src/main.rs b/examples/customize-extractor-error/src/main.rs index bc0973b4..1ba9481f 100644 --- a/examples/customize-extractor-error/src/main.rs +++ b/examples/customize-extractor-error/src/main.rs @@ -27,7 +27,7 @@ async fn main() { .init(); // build our application with a route - let app = Router::new().route("/users", post(handler)); + let app = Router::without_state().route("/users", post(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -53,17 +53,18 @@ struct User { struct Json(T); #[async_trait] -impl FromRequest for Json +impl FromRequest for Json where // these trait bounds are copied from `impl FromRequest for axum::Json` T: DeserializeOwned, B: axum::body::HttpBody + Send, B::Data: Send, B::Error: Into, + S: Send, { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { match axum::Json::::from_request(req).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { diff --git a/examples/customize-path-rejection/src/main.rs b/examples/customize-path-rejection/src/main.rs index 4e268949..fbdb13df 100644 --- a/examples/customize-path-rejection/src/main.rs +++ b/examples/customize-path-rejection/src/main.rs @@ -27,7 +27,7 @@ async fn main() { .init(); // build our application with a route - let app = Router::new().route("/users/:user_id/teams/:team_id", get(handler)); + let app = Router::without_state().route("/users/:user_id/teams/:team_id", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -52,15 +52,16 @@ struct Params { struct Path(T); #[async_trait] -impl FromRequest for Path +impl FromRequest for Path where // these trait bounds are copied from `impl FromRequest for axum::extract::path::Path` T: DeserializeOwned + Send, B: Send, + S: Send, { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { match axum::extract::Path::::from_request(req).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { diff --git a/examples/error-handling-and-dependency-injection/src/main.rs b/examples/error-handling-and-dependency-injection/src/main.rs index d92b43bf..c1cd4a63 100644 --- a/examples/error-handling-and-dependency-injection/src/main.rs +++ b/examples/error-handling-and-dependency-injection/src/main.rs @@ -36,7 +36,7 @@ async fn main() { let user_repo = Arc::new(ExampleUserRepo) as DynUserRepo; // Build our application with some routes - let app = Router::new() + let app = Router::without_state() .route("/users/:id", get(users_show)) .route("/users", post(users_create)) // Add our `user_repo` to all request's extensions so handlers can access diff --git a/examples/form/src/main.rs b/examples/form/src/main.rs index 7605baff..30cea959 100644 --- a/examples/form/src/main.rs +++ b/examples/form/src/main.rs @@ -19,7 +19,7 @@ async fn main() { .init(); // build our application with some routes - let app = Router::new().route("/", get(show_form).post(accept_form)); + let app = Router::without_state().route("/", get(show_form).post(accept_form)); // run it with hyper let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/global-404-handler/src/main.rs b/examples/global-404-handler/src/main.rs index 385a0e21..3371fe34 100644 --- a/examples/global-404-handler/src/main.rs +++ b/examples/global-404-handler/src/main.rs @@ -5,7 +5,6 @@ //! ``` use axum::{ - handler::Handler, http::StatusCode, response::{Html, IntoResponse}, routing::get, @@ -24,10 +23,10 @@ async fn main() { .init(); // build our application with a route - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); // add a fallback service for handling routes to unknown paths - let app = app.fallback(handler_404.into_service()); + let app = app.fallback(handler_404); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/graceful-shutdown/src/main.rs b/examples/graceful-shutdown/src/main.rs index f4a0fc29..defc740b 100644 --- a/examples/graceful-shutdown/src/main.rs +++ b/examples/graceful-shutdown/src/main.rs @@ -12,7 +12,7 @@ use tokio::signal; #[tokio::main] async fn main() { // build our application with a route - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs index ed115f6b..68f39a36 100644 --- a/examples/hello-world/src/main.rs +++ b/examples/hello-world/src/main.rs @@ -10,7 +10,7 @@ use std::net::SocketAddr; #[tokio::main] async fn main() { // build our application with a route - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/http-proxy/src/main.rs b/examples/http-proxy/src/main.rs index a4b412bb..27e160d4 100644 --- a/examples/http-proxy/src/main.rs +++ b/examples/http-proxy/src/main.rs @@ -35,7 +35,7 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let router = Router::new().route("/", get(|| async { "Hello, World!" })); + let router = Router::without_state().route("/", get(|| async { "Hello, World!" })); let service = tower::service_fn(move |req: Request| { let router = router.clone(); diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 0ac4053e..a7cd0577 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -62,7 +62,7 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let app = Router::new() + let app = Router::without_state() .route("/protected", get(protected)) .route("/authorize", post(authorize)); @@ -122,13 +122,14 @@ impl AuthBody { } #[async_trait] -impl FromRequest for Claims +impl FromRequest for Claims where + S: Send, B: Send, { type Rejection = AuthError; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { // Extract the token from the authorization header let TypedHeader(Authorization(bearer)) = TypedHeader::>::from_request(req) diff --git a/examples/key-value-store/src/main.rs b/examples/key-value-store/src/main.rs index 0ad5b7f6..c8150eca 100644 --- a/examples/key-value-store/src/main.rs +++ b/examples/key-value-store/src/main.rs @@ -40,7 +40,7 @@ async fn main() { .init(); // Build our application by composing routes - let app = Router::new() + let app = Router::without_state() .route( "/:key", // Add compression to `kv_get` @@ -110,7 +110,7 @@ async fn list_keys(Extension(state): Extension) -> String { .join("\n") } -fn admin_routes() -> Router { +fn admin_routes() -> Router<()> { async fn delete_all_keys(Extension(state): Extension) { state.write().unwrap().db.clear(); } diff --git a/examples/low-level-rustls/src/main.rs b/examples/low-level-rustls/src/main.rs index 6f4a2259..7802822f 100644 --- a/examples/low-level-rustls/src/main.rs +++ b/examples/low-level-rustls/src/main.rs @@ -50,7 +50,7 @@ async fn main() { let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap(); let mut listener = AddrIncoming::from_listener(listener).unwrap(); - let mut app = Router::new() + let mut app = Router::without_state() .route("/", get(handler)) .into_make_service_with_connect_info::(); diff --git a/examples/multipart-form/src/main.rs b/examples/multipart-form/src/main.rs index 7c6c1410..293be2a7 100644 --- a/examples/multipart-form/src/main.rs +++ b/examples/multipart-form/src/main.rs @@ -24,7 +24,7 @@ async fn main() { .init(); // build our application with some routes - let app = Router::new() + let app = Router::without_state() .route("/", get(show_form).post(accept_form)) .layer(tower_http::trace::TraceLayer::new_for_http()); diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 6357a7fc..250ef9a0 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -45,7 +45,7 @@ async fn main() { let oauth_client = oauth_client(); - let app = Router::new() + let app = Router::without_state() .route("/", get(index)) .route("/auth/discord", get(discord_auth)) .route("/auth/authorized", get(login_authorized)) @@ -205,14 +205,15 @@ impl IntoResponse for AuthRedirect { } #[async_trait] -impl FromRequest for User +impl FromRequest for User where + S: Send, B: Send, { // If anything goes wrong or no session is found, redirect to the auth page type Rejection = AuthRedirect; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(store) = Extension::::from_request(req) .await .expect("`MemoryStore` extension is missing"); diff --git a/examples/print-request-response/src/main.rs b/examples/print-request-response/src/main.rs index 59c609dd..7610f43c 100644 --- a/examples/print-request-response/src/main.rs +++ b/examples/print-request-response/src/main.rs @@ -25,7 +25,7 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let app = Router::new() + let app = Router::without_state() .route("/", post(|| async move { "Hello from `POST /`" })) .layer(middleware::from_fn(print_request_response)); diff --git a/examples/prometheus-metrics/src/main.rs b/examples/prometheus-metrics/src/main.rs index 1152b3c7..b14c93ec 100644 --- a/examples/prometheus-metrics/src/main.rs +++ b/examples/prometheus-metrics/src/main.rs @@ -35,7 +35,7 @@ async fn main() { let recorder_handle = setup_metrics_recorder(); - let app = Router::new() + let app = Router::without_state() .route("/fast", get(|| async {})) .route( "/slow", diff --git a/examples/query-params-with-empty-strings/src/main.rs b/examples/query-params-with-empty-strings/src/main.rs index 0af20111..3d4ab36c 100644 --- a/examples/query-params-with-empty-strings/src/main.rs +++ b/examples/query-params-with-empty-strings/src/main.rs @@ -4,7 +4,7 @@ //! cd examples && cargo run -p example-query-params-with-empty-strings //! ``` -use axum::{extract::Query, routing::get, Router}; +use axum::{extract::Query, routing::{get, WithState}, Router}; use serde::{de, Deserialize, Deserializer}; use std::{fmt, str::FromStr}; @@ -16,8 +16,8 @@ async fn main() { .unwrap(); } -fn app() -> Router { - Router::new().route("/", get(handler)) +fn app() -> Router<(), WithState> { + Router::without_state().route("/", get(handler)) } async fn handler(Query(params): Query) -> String { diff --git a/examples/readme/src/main.rs b/examples/readme/src/main.rs index a606893d..fb560c53 100644 --- a/examples/readme/src/main.rs +++ b/examples/readme/src/main.rs @@ -19,7 +19,7 @@ async fn main() { tracing_subscriber::fmt::init(); // build our application with a route - let app = Router::new() + let app = Router::without_state() // `GET /` goes to `root` .route("/", get(root)) // `POST /users` goes to `create_user` diff --git a/examples/rest-grpc-multiplex/src/main.rs b/examples/rest-grpc-multiplex/src/main.rs index 8376fcb0..58a6c4c2 100644 --- a/examples/rest-grpc-multiplex/src/main.rs +++ b/examples/rest-grpc-multiplex/src/main.rs @@ -55,7 +55,7 @@ async fn main() { .init(); // build the rest service - let rest = Router::new().route("/", get(web_root)); + let rest = Router::without_state().route("/", get(web_root)); // build the grpc service let grpc = GreeterServer::new(GrpcServiceImpl::default()); diff --git a/examples/reverse-proxy/src/main.rs b/examples/reverse-proxy/src/main.rs index a9d2a5c7..b4d6888c 100644 --- a/examples/reverse-proxy/src/main.rs +++ b/examples/reverse-proxy/src/main.rs @@ -24,7 +24,7 @@ async fn main() { let client = Client::new(); - let app = Router::new() + let app = Router::without_state() .route("/", get(handler)) .layer(Extension(client)); @@ -57,7 +57,7 @@ async fn handler( } async fn server() { - let app = Router::new().route("/", get(|| async { "Hello, world!" })); + let app = Router::without_state().route("/", get(|| async { "Hello, world!" })); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); println!("server listening on {}", addr); diff --git a/examples/sessions/src/main.rs b/examples/sessions/src/main.rs index 3251122c..631acb20 100644 --- a/examples/sessions/src/main.rs +++ b/examples/sessions/src/main.rs @@ -38,7 +38,7 @@ async fn main() { // `MemoryStore` just used as an example. Don't use this in production. let store = MemoryStore::new(); - let app = Router::new() + let app = Router::without_state() .route("/", get(handler)) .layer(Extension(store)); @@ -82,13 +82,14 @@ enum UserIdFromSession { } #[async_trait] -impl FromRequest for UserIdFromSession +impl FromRequest for UserIdFromSession where B: Send, + S: Send, { type Rejection = (StatusCode, &'static str); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(store) = Extension::::from_request(req) .await .expect("`MemoryStore` extension missing"); diff --git a/examples/sqlx-postgres/src/main.rs b/examples/sqlx-postgres/src/main.rs index 9d101618..5eaf4132 100644 --- a/examples/sqlx-postgres/src/main.rs +++ b/examples/sqlx-postgres/src/main.rs @@ -46,7 +46,7 @@ async fn main() { .expect("can connect to database"); // build our application with some routes - let app = Router::new() + let app = Router::without_state() .route( "/", get(using_connection_pool_extractor).post(using_connection_extractor), @@ -77,13 +77,14 @@ async fn using_connection_pool_extractor( struct DatabaseConnection(sqlx::pool::PoolConnection); #[async_trait] -impl FromRequest for DatabaseConnection +impl FromRequest for DatabaseConnection where + S: Send, B: Send, { type Rejection = (StatusCode, String); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(pool) = Extension::::from_request(req) .await .map_err(internal_error)?; diff --git a/examples/sse/src/main.rs b/examples/sse/src/main.rs index 4dbfcb46..58c7f402 100644 --- a/examples/sse/src/main.rs +++ b/examples/sse/src/main.rs @@ -29,19 +29,19 @@ async fn main() { let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"); - let static_files_service = get_service( - ServeDir::new(assets_dir).append_index_html_on_directories(true), - ) - .handle_error(|error: std::io::Error| async move { - ( - StatusCode::INTERNAL_SERVER_ERROR, - format!("Unhandled internal error: {}", error), - ) - }); + let static_files_service = + get_service(ServeDir::new(assets_dir).append_index_html_on_directories(true)) + .handle_error(|error: std::io::Error| async move { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Unhandled internal error: {}", error), + ) + }) + .state(()); // build our application with a route - let app = Router::new() - .fallback(static_files_service) + let app = Router::without_state() + .fallback_service(static_files_service) .route("/sse", get(sse_handler)) .layer(TraceLayer::new_for_http()); diff --git a/examples/static-file-server/src/main.rs b/examples/static-file-server/src/main.rs index 1862ecab..ca82d774 100644 --- a/examples/static-file-server/src/main.rs +++ b/examples/static-file-server/src/main.rs @@ -32,9 +32,13 @@ async fn main() { // for serving assets directly at the root you can use `tower_http::services::ServeDir` // as the fallback to a `Router` - let app: _ = Router::new() + let app: _ = Router::without_state() .route("/foo", get(|| async { "Hi from /foo" })) - .fallback(get_service(ServeDir::new(".")).handle_error(handle_error)) + .fallback_service( + get_service(ServeDir::new(".")) + .handle_error(handle_error) + .state(()), + ) .layer(TraceLayer::new_for_http()); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/stream-to-file/src/main.rs b/examples/stream-to-file/src/main.rs index e9b816b1..ba933c7a 100644 --- a/examples/stream-to-file/src/main.rs +++ b/examples/stream-to-file/src/main.rs @@ -34,7 +34,7 @@ async fn main() { .await .expect("failed to create `uploads` directory"); - let app = Router::new() + let app = Router::without_state() .route("/", get(show_form).post(accept_form)) .route("/file/:file_name", post(save_request_body)); diff --git a/examples/templates/src/main.rs b/examples/templates/src/main.rs index 90ea206b..675adedd 100644 --- a/examples/templates/src/main.rs +++ b/examples/templates/src/main.rs @@ -25,7 +25,7 @@ async fn main() { .init(); // build our application with some routes - let app = Router::new().route("/greet/:name", get(greet)); + let app = Router::without_state().route("/greet/:name", get(greet)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 2893188b..83eb6d04 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -5,7 +5,7 @@ //! ``` use axum::{ - routing::{get, post}, + routing::{get, post, WithState}, Json, Router, }; use tower_http::trace::TraceLayer; @@ -34,8 +34,8 @@ async fn main() { /// Having a function that produces our app makes it easy to call it from tests /// without having to create an HTTP server. #[allow(dead_code)] -fn app() -> Router { - Router::new() +fn app() -> Router<(), WithState> { + Router::without_state() .route("/", get(|| async { "Hello, World!" })) .route( "/json", diff --git a/examples/tls-rustls/src/main.rs b/examples/tls-rustls/src/main.rs index b3110675..278de213 100644 --- a/examples/tls-rustls/src/main.rs +++ b/examples/tls-rustls/src/main.rs @@ -29,7 +29,7 @@ async fn main() { .await .unwrap(); - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); println!("listening on {}", addr); diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 9a33416b..027e0cec 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -46,7 +46,7 @@ async fn main() { let db = Db::default(); // Compose the routes - let app = Router::new() + let app = Router::without_state() .route("/todos", get(todos_index).post(todos_create)) .route("/todos/:id", patch(todos_update).delete(todos_delete)) // Add middleware to all routes diff --git a/examples/tokio-postgres/src/main.rs b/examples/tokio-postgres/src/main.rs index 4489f616..3ad9a80d 100644 --- a/examples/tokio-postgres/src/main.rs +++ b/examples/tokio-postgres/src/main.rs @@ -33,7 +33,7 @@ async fn main() { let pool = Pool::builder().build(manager).await.unwrap(); // build our application with some routes - let app = Router::new() + let app = Router::without_state() .route( "/", get(using_connection_pool_extractor).post(using_connection_extractor), @@ -71,13 +71,14 @@ async fn using_connection_pool_extractor( struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager>); #[async_trait] -impl FromRequest for DatabaseConnection +impl FromRequest for DatabaseConnection where B: Send, + S: Send, { type Rejection = (StatusCode, String); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(pool) = Extension::::from_request(req) .await .map_err(internal_error)?; diff --git a/examples/tracing-aka-logging/src/main.rs b/examples/tracing-aka-logging/src/main.rs index 8da6e5fe..dbb1e982 100644 --- a/examples/tracing-aka-logging/src/main.rs +++ b/examples/tracing-aka-logging/src/main.rs @@ -27,7 +27,7 @@ async fn main() { .init(); // build our application with a route - let app = Router::new() + let app = Router::without_state() .route("/", get(handler)) // `TraceLayer` is provided by tower-http so you have to add that as a dependency. // It provides good defaults but is also very customizable. diff --git a/examples/unix-domain-socket/src/main.rs b/examples/unix-domain-socket/src/main.rs index 2f67a4c1..95ca9ad1 100644 --- a/examples/unix-domain-socket/src/main.rs +++ b/examples/unix-domain-socket/src/main.rs @@ -60,7 +60,7 @@ mod unix { let uds = UnixListener::bind(path.clone()).unwrap(); tokio::spawn(async { - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); axum::Server::builder(ServerAccept { uds }) .serve(app.into_make_service_with_connect_info::()) diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs index c8ce8c08..81496be4 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -34,7 +34,7 @@ async fn main() { .init(); // build our application with a route - let app = Router::new().route("/", get(handler)); + let app = Router::without_state().route("/", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -60,16 +60,17 @@ async fn handler(ValidatedForm(input): ValidatedForm) -> Html pub struct ValidatedForm(pub T); #[async_trait] -impl FromRequest for ValidatedForm +impl FromRequest for ValidatedForm where T: DeserializeOwned + Validate, B: http_body::Body + Send, B::Data: Send, B::Error: Into, + S: Send, { type Rejection = ServerError; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Form(value) = Form::::from_request(req).await?; value.validate()?; Ok(ValidatedForm(value)) diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 48ade3c9..d3acb026 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -25,7 +25,7 @@ async fn main() { .init(); // build our application with some routes - let app = Router::new().route("/:version/foo", get(handler)); + let app = Router::without_state().route("/:version/foo", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -48,13 +48,14 @@ enum Version { } #[async_trait] -impl FromRequest for Version +impl FromRequest for Version where B: Send, + S: Send, { type Rejection = Response; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let params = Path::>::from_request(req) .await .map_err(IntoResponse::into_response)?; diff --git a/examples/websockets/src/main.rs b/examples/websockets/src/main.rs index bbdcaa08..42cc6bb9 100644 --- a/examples/websockets/src/main.rs +++ b/examples/websockets/src/main.rs @@ -36,15 +36,16 @@ async fn main() { let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets"); // build our application with some routes - let app = Router::new() - .fallback( + let app = Router::without_state() + .fallback_service( get_service(ServeDir::new(assets_dir).append_index_html_on_directories(true)) .handle_error(|error: std::io::Error| async move { ( StatusCode::INTERNAL_SERVER_ERROR, format!("Unhandled internal error: {}", error), ) - }), + }) + .state(()), ) // routes are matched from bottom to top, so we have to put `nest` at the // top since it matches all routes