summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/consume-body-in-extractor-or-middleware/src/main.rs13
-rw-r--r--examples/customize-extractor-error/src/custom_extractor.rs6
-rw-r--r--examples/http-proxy/src/main.rs9
-rw-r--r--examples/low-level-rustls/src/main.rs4
-rw-r--r--examples/parse-body-based-on-content-type/src/main.rs7
-rw-r--r--examples/print-request-response/src/main.rs7
-rw-r--r--examples/prometheus-metrics/src/main.rs5
-rw-r--r--examples/rest-grpc-multiplex/src/multiplex_service.rs17
-rw-r--r--examples/reverse-proxy/src/main.rs6
-rw-r--r--examples/static-file-server/src/main.rs8
-rw-r--r--examples/stream-to-file/src/main.rs8
-rw-r--r--examples/testing/src/main.rs15
-rw-r--r--examples/validator/src/main.rs7
13 files changed, 50 insertions, 62 deletions
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 25819a22..f1b2cbf7 100644
--- a/examples/consume-body-in-extractor-or-middleware/src/main.rs
+++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs
@@ -7,8 +7,8 @@
use axum::{
async_trait,
body::{Body, Bytes},
- extract::FromRequest,
- http::{Request, StatusCode},
+ extract::{FromRequest, Request},
+ http::StatusCode,
middleware::{self, Next},
response::{IntoResponse, Response},
routing::post,
@@ -41,10 +41,7 @@ async fn main() {
}
// middleware that shows how to consume the request body upfront
-async fn print_request_body(
- request: Request<Body>,
- next: Next<Body>,
-) -> Result<impl IntoResponse, Response> {
+async fn print_request_body(request: Request, next: Next) -> Result<impl IntoResponse, Response> {
let request = buffer_request_body(request).await?;
Ok(next.run(request).await)
@@ -52,7 +49,7 @@ async fn print_request_body(
// the trick is to take the request apart, buffer the body, do what you need to do, then put
// the request back together
-async fn buffer_request_body(request: Request<Body>) -> Result<Request<Body>, Response> {
+async fn buffer_request_body(request: Request) -> Result<Request, Response> {
let (parts, body) = request.into_parts();
// this wont work if the body is an long running stream
@@ -84,7 +81,7 @@ where
{
type Rejection = Response;
- async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
+ async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let body = Bytes::from_request(req, state)
.await
.map_err(|err| err.into_response())?;
diff --git a/examples/customize-extractor-error/src/custom_extractor.rs b/examples/customize-extractor-error/src/custom_extractor.rs
index 6a39ffbd..3611fba7 100644
--- a/examples/customize-extractor-error/src/custom_extractor.rs
+++ b/examples/customize-extractor-error/src/custom_extractor.rs
@@ -6,9 +6,7 @@
//! - Complexity: Manually implementing `FromRequest` results on more complex code
use axum::{
async_trait,
- body::Body,
- extract::{rejection::JsonRejection, FromRequest, MatchedPath},
- http::Request,
+ extract::{rejection::JsonRejection, FromRequest, MatchedPath, Request},
http::StatusCode,
response::IntoResponse,
RequestPartsExt,
@@ -30,7 +28,7 @@ where
{
type Rejection = (StatusCode, axum::Json<Value>);
- async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
+ async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, body) = req.into_parts();
// We can use other extractors to provide better rejection messages.
diff --git a/examples/http-proxy/src/main.rs b/examples/http-proxy/src/main.rs
index 73abf78f..08845ca9 100644
--- a/examples/http-proxy/src/main.rs
+++ b/examples/http-proxy/src/main.rs
@@ -13,8 +13,9 @@
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
use axum::{
- body::{self, Body},
- http::{Method, Request, StatusCode},
+ body::Body,
+ extract::Request,
+ http::{Method, StatusCode},
response::{IntoResponse, Response},
routing::get,
Router,
@@ -59,7 +60,7 @@ async fn main() {
.unwrap();
}
-async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
+async fn proxy(req: Request) -> Result<Response, hyper::Error> {
tracing::trace!(?req);
if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) {
@@ -74,7 +75,7 @@ async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
}
});
- Ok(Response::new(body::boxed(body::Empty::new())))
+ Ok(Response::new(Body::empty()))
} else {
tracing::warn!("CONNECT host is not socket addr: {:?}", req.uri());
Ok((
diff --git a/examples/low-level-rustls/src/main.rs b/examples/low-level-rustls/src/main.rs
index afe6a037..1e9a951f 100644
--- a/examples/low-level-rustls/src/main.rs
+++ b/examples/low-level-rustls/src/main.rs
@@ -4,7 +4,7 @@
//! cargo run -p example-low-level-rustls
//! ```
-use axum::{body::Body, extract::ConnectInfo, http::Request, routing::get, Router};
+use axum::{extract::ConnectInfo, extract::Request, routing::get, Router};
use futures_util::future::poll_fn;
use hyper::server::{
accept::Accept,
@@ -67,7 +67,7 @@ async fn main() {
let protocol = protocol.clone();
- let svc = MakeService::<_, Request<Body>>::make_service(&mut app, &stream);
+ let svc = MakeService::<_, Request<hyper::Body>>::make_service(&mut app, &stream);
tokio::spawn(async move {
if let Ok(stream) = acceptor.accept(stream).await {
diff --git a/examples/parse-body-based-on-content-type/src/main.rs b/examples/parse-body-based-on-content-type/src/main.rs
index d66791bc..9ee26c6a 100644
--- a/examples/parse-body-based-on-content-type/src/main.rs
+++ b/examples/parse-body-based-on-content-type/src/main.rs
@@ -8,9 +8,8 @@
use axum::{
async_trait,
- body::Body,
- extract::FromRequest,
- http::{header::CONTENT_TYPE, Request, StatusCode},
+ extract::{FromRequest, Request},
+ http::{header::CONTENT_TYPE, StatusCode},
response::{IntoResponse, Response},
routing::post,
Form, Json, RequestExt, Router,
@@ -61,7 +60,7 @@ where
{
type Rejection = Response;
- async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
+ async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let content_type_header = req.headers().get(CONTENT_TYPE);
let content_type = content_type_header.and_then(|value| value.to_str().ok());
diff --git a/examples/print-request-response/src/main.rs b/examples/print-request-response/src/main.rs
index c071ff54..4703c058 100644
--- a/examples/print-request-response/src/main.rs
+++ b/examples/print-request-response/src/main.rs
@@ -6,7 +6,8 @@
use axum::{
body::{Body, Bytes},
- http::{Request, StatusCode},
+ extract::Request,
+ http::StatusCode,
middleware::{self, Next},
response::{IntoResponse, Response},
routing::post,
@@ -38,8 +39,8 @@ async fn main() {
}
async fn print_request_response(
- req: Request<Body>,
- next: Next<Body>,
+ req: Request,
+ next: Next,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let (parts, body) = req.into_parts();
let bytes = buffer_and_print("request", body).await?;
diff --git a/examples/prometheus-metrics/src/main.rs b/examples/prometheus-metrics/src/main.rs
index cf7db5ac..675310be 100644
--- a/examples/prometheus-metrics/src/main.rs
+++ b/examples/prometheus-metrics/src/main.rs
@@ -8,8 +8,7 @@
//! ```
use axum::{
- extract::MatchedPath,
- http::Request,
+ extract::{MatchedPath, Request},
middleware::{self, Next},
response::IntoResponse,
routing::get,
@@ -94,7 +93,7 @@ fn setup_metrics_recorder() -> PrometheusHandle {
.unwrap()
}
-async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
+async fn track_metrics(req: Request, next: Next) -> impl IntoResponse {
let start = Instant::now();
let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() {
matched_path.as_str().to_owned()
diff --git a/examples/rest-grpc-multiplex/src/multiplex_service.rs b/examples/rest-grpc-multiplex/src/multiplex_service.rs
index 0f08dbf6..777c14cd 100644
--- a/examples/rest-grpc-multiplex/src/multiplex_service.rs
+++ b/examples/rest-grpc-multiplex/src/multiplex_service.rs
@@ -1,6 +1,9 @@
-use axum::{body::BoxBody, http::header::CONTENT_TYPE, response::IntoResponse};
+use axum::{
+ extract::Request,
+ http::header::CONTENT_TYPE,
+ response::{IntoResponse, Response},
+};
use futures::{future::BoxFuture, ready};
-use hyper::{Body, Request, Response};
use std::{
convert::Infallible,
task::{Context, Poll},
@@ -41,16 +44,16 @@ where
}
}
-impl<A, B> Service<Request<Body>> for MultiplexService<A, B>
+impl<A, B> Service<Request<hyper::Body>> for MultiplexService<A, B>
where
- A: Service<Request<Body>, Error = Infallible>,
+ A: Service<Request<hyper::Body>, Error = Infallible>,
A::Response: IntoResponse,
A::Future: Send + 'static,
- B: Service<Request<Body>>,
+ B: Service<Request<hyper::Body>>,
B::Response: IntoResponse,
B::Future: Send + 'static,
{
- type Response = Response<BoxBody>;
+ type Response = Response;
type Error = B::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
@@ -73,7 +76,7 @@ where
}
}
- fn call(&mut self, req: Request<Body>) -> Self::Future {
+ fn call(&mut self, req: Request<hyper::Body>) -> Self::Future {
// require users to call `poll_ready` first, if they don't we're allowed to panic
// as per the `tower::Service` contract
assert!(
diff --git a/examples/reverse-proxy/src/main.rs b/examples/reverse-proxy/src/main.rs
index 634a6a04..a01947c6 100644
--- a/examples/reverse-proxy/src/main.rs
+++ b/examples/reverse-proxy/src/main.rs
@@ -9,8 +9,8 @@
use axum::{
body::Body,
- extract::State,
- http::{uri::Uri, Request},
+ extract::{Request, State},
+ http::uri::Uri,
response::{IntoResponse, Response},
routing::get,
Router,
@@ -36,7 +36,7 @@ async fn main() {
.unwrap();
}
-async fn handler(State(client): State<Client>, mut req: Request<Body>) -> Response {
+async fn handler(State(client): State<Client>, mut req: Request) -> Response {
let path = req.uri().path();
let path_query = req
.uri()
diff --git a/examples/static-file-server/src/main.rs b/examples/static-file-server/src/main.rs
index 50857d96..3a3a2414 100644
--- a/examples/static-file-server/src/main.rs
+++ b/examples/static-file-server/src/main.rs
@@ -5,11 +5,7 @@
//! ```
use axum::{
- body::Body,
- handler::HandlerWithoutStateExt,
- http::{Request, StatusCode},
- routing::get,
- Router,
+ extract::Request, handler::HandlerWithoutStateExt, http::StatusCode, routing::get, Router,
};
use std::net::SocketAddr;
use tower::ServiceExt;
@@ -97,7 +93,7 @@ fn calling_serve_dir_from_a_handler() -> Router {
// call `ServeDir` yourself from a handler
Router::new().nest_service(
"/foo",
- get(|request: Request<Body>| async {
+ get(|request: Request| async {
let service = ServeDir::new("assets");
let result = service.oneshot(request).await;
result
diff --git a/examples/stream-to-file/src/main.rs b/examples/stream-to-file/src/main.rs
index e58fd149..2d514162 100644
--- a/examples/stream-to-file/src/main.rs
+++ b/examples/stream-to-file/src/main.rs
@@ -5,9 +5,9 @@
//! ```
use axum::{
- body::{Body, Bytes},
- extract::{Multipart, Path},
- http::{Request, StatusCode},
+ body::Bytes,
+ extract::{Multipart, Path, Request},
+ http::StatusCode,
response::{Html, Redirect},
routing::{get, post},
BoxError, Router,
@@ -52,7 +52,7 @@ async fn main() {
// POST'ing to `/file/foo.txt` will create a file called `foo.txt`.
async fn save_request_body(
Path(file_name): Path<String>,
- request: Request<Body>,
+ request: Request,
) -> Result<(), (StatusCode, String)> {
stream_to_file(&file_name, request.into_body()).await
}
diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs
index 02079eb8..5f7dbc07 100644
--- a/examples/testing/src/main.rs
+++ b/examples/testing/src/main.rs
@@ -162,7 +162,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 = ServiceExt::<Request<Body>>::ready(&mut app)
@@ -190,20 +190,15 @@ mod tests {
// tests.
#[tokio::test]
async fn with_into_make_service_with_connect_info() {
- let mut app = app().layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))));
+ let mut app = app()
+ .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
+ .into_service();
let request = Request::builder()
.uri("/requires-connect-into")
.body(Body::empty())
.unwrap();
- let response = app
- .as_service()
- .ready()
- .await
- .unwrap()
- .call(request)
- .await
- .unwrap();
+ let response = app.ready().await.unwrap().call(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
}
diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs
index 8545a3e9..4f4f6239 100644
--- a/examples/validator/src/main.rs
+++ b/examples/validator/src/main.rs
@@ -12,9 +12,8 @@
use async_trait::async_trait;
use axum::{
- body::Body,
- extract::{rejection::FormRejection, Form, FromRequest},
- http::{Request, StatusCode},
+ extract::{rejection::FormRejection, Form, FromRequest, Request},
+ http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
Router,
@@ -70,7 +69,7 @@ where
{
type Rejection = ServerError;
- async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
+ async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let Form(value) = Form::<T>::from_request(req, state).await?;
value.validate()?;
Ok(ValidatedForm(value))