Remove axum::prelude (#195)

This commit is contained in:
Florian Thelliez
2021-08-18 00:04:15 +02:00
committed by GitHub
parent f083c3e97e
commit d9a06ef14b
43 changed files with 529 additions and 171 deletions
+6 -3
View File
@@ -6,7 +6,10 @@
use axum::{
body::{box_body, Body, BoxBody},
prelude::*,
handler::get,
response::Html,
route,
routing::RoutingDsl,
};
use http::{Response, StatusCode};
use std::net::SocketAddr;
@@ -34,8 +37,8 @@ async fn main() {
.unwrap();
}
async fn handler() -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
fn map_404(response: Response<BoxBody>) -> Response<BoxBody> {
+6 -6
View File
@@ -3,18 +3,18 @@ mod starwars;
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{EmptyMutation, EmptySubscription, Request, Response, Schema};
use axum::response::IntoResponse;
use axum::{prelude::*, AddExtensionLayer};
use axum::{
extract::Extension, handler::get, response::Html, route, routing::RoutingDsl,
AddExtensionLayer, Json,
};
use starwars::{QueryRoot, StarWars, StarWarsSchema};
async fn graphql_handler(
schema: extract::Extension<StarWarsSchema>,
req: extract::Json<Request>,
) -> response::Json<Response> {
async fn graphql_handler(schema: Extension<StarWarsSchema>, req: Json<Request>) -> Json<Response> {
schema.execute(req.0).await.into()
}
async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(GraphQLPlaygroundConfig::new("/")))
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
}
#[tokio::main]
+8 -3
View File
@@ -14,9 +14,14 @@ use futures::{sink::SinkExt, stream::StreamExt};
use tokio::sync::broadcast;
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::prelude::*;
use axum::extract::{
ws::{Message, WebSocket, WebSocketUpgrade},
Extension,
};
use axum::handler::get;
use axum::response::{Html, IntoResponse};
use axum::route;
use axum::routing::RoutingDsl;
use axum::AddExtensionLayer;
// Our shared state
@@ -46,7 +51,7 @@ async fn main() {
async fn websocket_handler(
ws: WebSocketUpgrade,
extract::Extension(state): extract::Extension<Arc<AppState>>,
Extension(state): Extension<Arc<AppState>>,
) -> impl IntoResponse {
ws.on_upgrade(|socket| websocket(socket, state))
}
@@ -11,10 +11,12 @@
use axum::{
async_trait,
extract::{Extension, Json, Path},
prelude::*,
extract::{Extension, Path},
handler::{get, post},
response::IntoResponse,
AddExtensionLayer,
route,
routing::RoutingDsl,
AddExtensionLayer, Json,
};
use bytes::Bytes;
use http::{Response, StatusCode};
@@ -60,7 +62,7 @@ async fn main() {
async fn users_show(
Path(user_id): Path<Uuid>,
Extension(user_repo): Extension<DynUserRepo>,
) -> Result<response::Json<User>, AppError> {
) -> Result<Json<User>, AppError> {
let user = user_repo.find(user_id).await?;
Ok(user.into())
@@ -70,7 +72,7 @@ async fn users_show(
async fn users_create(
Json(params): Json<CreateUser>,
Extension(user_repo): Extension<DynUserRepo>,
) -> Result<response::Json<User>, AppError> {
) -> Result<Json<User>, AppError> {
let user = user_repo.create(params).await?;
Ok(user.into())
@@ -104,7 +106,7 @@ impl IntoResponse for AppError {
}
};
let mut response = response::Json(json!({
let mut response = Json(json!({
"error": error_json,
}))
.into_response();
+4 -4
View File
@@ -4,7 +4,7 @@
//! cargo run --example form
//! ```
use axum::prelude::*;
use axum::{extract::Form, handler::get, response::Html, route, routing::RoutingDsl};
use serde::Deserialize;
use std::net::SocketAddr;
@@ -28,8 +28,8 @@ async fn main() {
.unwrap();
}
async fn show_form() -> response::Html<&'static str> {
response::Html(
async fn show_form() -> Html<&'static str> {
Html(
r#"
<!doctype html>
<html>
@@ -60,6 +60,6 @@ struct Input {
email: String,
}
async fn accept_form(extract::Form(input): extract::Form<Input>) {
async fn accept_form(Form(input): Form<Input>) {
dbg!(&input);
}
+3 -3
View File
@@ -4,7 +4,7 @@
//! cargo run --example hello_world
//! ```
use axum::prelude::*;
use axum::{handler::get, response::Html, route, routing::RoutingDsl};
use std::net::SocketAddr;
#[tokio::main]
@@ -27,6 +27,6 @@ async fn main() {
.unwrap();
}
async fn handler() -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
+3 -2
View File
@@ -8,9 +8,10 @@
use axum::{
extract::{ContentLengthLimit, Extension, Path},
prelude::*,
handler::{delete, get, Handler},
response::IntoResponse,
routing::BoxRoute,
route,
routing::{BoxRoute, RoutingDsl},
};
use bytes::Bytes;
use http::StatusCode;
+6 -3
View File
@@ -6,7 +6,10 @@
use axum::{
extract::{ContentLengthLimit, Multipart},
prelude::*,
handler::get,
response::Html,
route,
routing::RoutingDsl,
};
use std::net::SocketAddr;
@@ -31,8 +34,8 @@ async fn main() {
.unwrap();
}
async fn show_form() -> response::Html<&'static str> {
response::Html(
async fn show_form() -> Html<&'static str> {
Html(
r#"
<!doctype html>
<html>
+5 -3
View File
@@ -11,8 +11,10 @@ use axum::{
async_trait,
body::{Bytes, Empty},
extract::{Extension, FromRequest, Query, RequestParts, TypedHeader},
prelude::*,
handler::get,
response::{IntoResponse, Redirect},
route,
routing::RoutingDsl,
AddExtensionLayer,
};
use http::{header::SET_COOKIE, HeaderMap};
@@ -212,11 +214,11 @@ where
type Rejection = AuthRedirect;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let extract::Extension(store) = extract::Extension::<MemoryStore>::from_request(req)
let Extension(store) = Extension::<MemoryStore>::from_request(req)
.await
.expect("`MemoryStore` extension is missing");
let cookies = extract::TypedHeader::<headers::Cookie>::from_request(req)
let cookies = TypedHeader::<headers::Cookie>::from_request(req)
.await
.expect("could not get cookies");
+5 -3
View File
@@ -7,9 +7,11 @@
use async_session::{MemoryStore, Session, SessionStore as _};
use axum::{
async_trait,
extract::{FromRequest, RequestParts},
prelude::*,
extract::{Extension, FromRequest, RequestParts},
handler::get,
response::IntoResponse,
route,
routing::RoutingDsl,
AddExtensionLayer,
};
use http::header::{HeaderMap, HeaderValue};
@@ -70,7 +72,7 @@ where
type Rejection = (StatusCode, &'static str);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let extract::Extension(store) = extract::Extension::<MemoryStore>::from_request(req)
let Extension(store) = Extension::<MemoryStore>::from_request(req)
.await
.expect("`MemoryStore` extension missing");
+2 -2
View File
@@ -6,9 +6,9 @@
use axum::{
extract::TypedHeader,
prelude::*,
handler::get,
response::sse::{sse, Event, Sse},
routing::nest,
routing::{nest, RoutingDsl},
};
use futures::stream::{self, Stream};
use http::StatusCode;
+1 -1
View File
@@ -4,7 +4,7 @@
//! cargo run --example static_file_server
//! ```
use axum::{prelude::*, routing::nest};
use axum::routing::{nest, RoutingDsl};
use http::StatusCode;
use std::net::SocketAddr;
use tower_http::{services::ServeDir, trace::TraceLayer};
+8 -2
View File
@@ -5,7 +5,13 @@
//! ```
use askama::Template;
use axum::{prelude::*, response::IntoResponse};
use axum::{
extract,
handler::get,
response::{Html, IntoResponse},
route,
routing::RoutingDsl,
};
use bytes::Bytes;
use http::{Response, StatusCode};
use http_body::Full;
@@ -53,7 +59,7 @@ where
fn into_response(self) -> Response<Self::Body> {
match self.0.render() {
Ok(html) => response::Html(html).into_response(),
Ok(html) => Html(html).into_response(),
Err(err) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::from(format!(
+10 -4
View File
@@ -4,7 +4,13 @@
//! cargo test --example testing
//! ```
use axum::{prelude::*, routing::BoxRoute};
use axum::{
body::Body,
handler::{get, post},
route,
routing::{BoxRoute, RoutingDsl},
Json,
};
use tower_http::trace::TraceLayer;
#[tokio::main]
@@ -32,8 +38,8 @@ fn app() -> BoxRoute<Body> {
route("/", get(|| async { "Hello, World!" }))
.route(
"/json",
post(|payload: extract::Json<serde_json::Value>| async move {
response::Json(serde_json::json!({ "data": payload.0 }))
post(|payload: Json<serde_json::Value>| async move {
Json(serde_json::json!({ "data": payload.0 }))
}),
)
// We can still add middleware
@@ -44,7 +50,7 @@ fn app() -> BoxRoute<Body> {
#[cfg(test)]
mod tests {
use super::*;
use http::StatusCode;
use http::{Request, StatusCode};
use serde_json::{json, Value};
use std::net::{SocketAddr, TcpListener};
use tower::ServiceExt; // for `app.oneshot()`
+1 -1
View File
@@ -4,7 +4,7 @@
//! cargo run --example tls_rustls
//! ```
use axum::prelude::*;
use axum::{handler::get, route};
use hyper::server::conn::Http;
use std::{fs::File, io::BufReader, sync::Arc};
use tokio::net::TcpListener;
+9 -6
View File
@@ -14,9 +14,12 @@
//! ```
use axum::{
extract::{Extension, Json, Path, Query},
prelude::*,
extract::{Extension, Path, Query},
handler::{get, patch},
response::IntoResponse,
route,
routing::RoutingDsl,
Json,
};
use http::StatusCode;
use serde::{Deserialize, Serialize};
@@ -95,10 +98,10 @@ async fn todos_index(
.values()
.cloned()
.skip(pagination.offset.unwrap_or(0))
.take(pagination.limit.unwrap_or(std::usize::MAX))
.take(pagination.limit.unwrap_or(usize::MAX))
.collect::<Vec<_>>();
response::Json(todos)
Json(todos)
}
#[derive(Debug, Deserialize)]
@@ -118,7 +121,7 @@ async fn todos_create(
db.write().unwrap().insert(todo.id, todo.clone());
(StatusCode::CREATED, response::Json(todo))
(StatusCode::CREATED, Json(todo))
}
#[derive(Debug, Deserialize)]
@@ -149,7 +152,7 @@ async fn todos_update(
db.write().unwrap().insert(todo.id, todo.clone());
Ok(response::Json(todo))
Ok(Json(todo))
}
async fn todos_delete(Path(id): Path<Uuid>, Extension(db): Extension<Db>) -> impl IntoResponse {
+3 -1
View File
@@ -7,7 +7,9 @@
use axum::{
async_trait,
extract::{Extension, FromRequest, RequestParts},
prelude::*,
handler::get,
route,
routing::RoutingDsl,
AddExtensionLayer,
};
use bb8::{Pool, PooledConnection};
+3 -3
View File
@@ -4,7 +4,7 @@
//! cargo run --example tracing_aka_logging
//! ```
use axum::prelude::*;
use axum::{handler::get, response::Html, route, routing::RoutingDsl};
use std::net::SocketAddr;
use tower_http::trace::TraceLayer;
@@ -32,6 +32,6 @@ async fn main() {
.unwrap();
}
async fn handler() -> response::Html<&'static str> {
response::Html("<h1>Hello, World!</h1>")
async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}
+5 -1
View File
@@ -5,8 +5,12 @@
//! ```
use axum::{
body::Body,
extract::connect_info::{self, ConnectInfo},
prelude::*,
handler::get,
http::Request,
route,
routing::RoutingDsl,
};
use futures::ready;
use http::{Method, StatusCode, Uri};
+6 -4
View File
@@ -4,11 +4,13 @@
//! cargo run --example versioning
//! ```
use axum::response::IntoResponse;
use axum::{
async_trait,
extract::{FromRequest, RequestParts},
prelude::*,
extract::{FromRequest, Path, RequestParts},
handler::get,
response::IntoResponse,
route,
routing::RoutingDsl,
};
use bytes::Bytes;
use http::Response;
@@ -56,7 +58,7 @@ where
type Rejection = Response<Full<Bytes>>;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = extract::Path::<HashMap<String, String>>::from_request(req)
let params = Path::<HashMap<String, String>>::from_request(req)
.await
.map_err(IntoResponse::into_response)?;
+2 -2
View File
@@ -11,9 +11,9 @@ use axum::{
ws::{Message, WebSocket, WebSocketUpgrade},
TypedHeader,
},
prelude::*,
handler::get,
response::IntoResponse,
routing::nest,
routing::{nest, RoutingDsl},
};
use http::StatusCode;
use std::net::SocketAddr;