Implement path extractor (#124)

Fixes #42
This commit is contained in:
Sunli
2021-08-06 10:17:57 +02:00
committed by GitHub
parent 2be79168d8
commit 9fdbd42fba
14 changed files with 871 additions and 66 deletions
@@ -11,7 +11,7 @@
use axum::{
async_trait,
extract::{Extension, Json, UrlParams},
extract::{Extension, Json, Path},
prelude::*,
response::IntoResponse,
AddExtensionLayer,
@@ -56,7 +56,7 @@ async fn main() {
/// are automatically converted into `AppError` which implements `IntoResponse`
/// so it can be returned from handlers directly.
async fn users_show(
UrlParams((user_id,)): UrlParams<(Uuid,)>,
Path(user_id): Path<Uuid>,
Extension(user_repo): Extension<DynUserRepo>,
) -> Result<response::Json<User>, AppError> {
let user = user_repo.find(user_id).await?;
+4 -7
View File
@@ -8,7 +8,7 @@
use axum::{
async_trait,
extract::{extractor_middleware, ContentLengthLimit, Extension, RequestParts, UrlParams},
extract::{extractor_middleware, ContentLengthLimit, Extension, Path, RequestParts},
prelude::*,
response::IntoResponse,
routing::BoxRoute,
@@ -79,7 +79,7 @@ struct State {
}
async fn kv_get(
UrlParams((key,)): UrlParams<(String,)>,
Path(key): Path<String>,
Extension(state): Extension<SharedState>,
) -> Result<Bytes, StatusCode> {
let db = &state.read().unwrap().db;
@@ -92,7 +92,7 @@ async fn kv_get(
}
async fn kv_set(
UrlParams((key,)): UrlParams<(String,)>,
Path(key): Path<String>,
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
) {
@@ -113,10 +113,7 @@ fn admin_routes() -> BoxRoute<hyper::Body> {
state.write().unwrap().db.clear();
}
async fn remove_key(
UrlParams((key,)): UrlParams<(String,)>,
Extension(state): Extension<SharedState>,
) {
async fn remove_key(Path(key): Path<String>, Extension(state): Extension<SharedState>) {
state.write().unwrap().db.remove(&key);
}
+1 -7
View File
@@ -29,14 +29,8 @@ async fn main() {
.unwrap();
}
async fn greet(params: extract::UrlParamsMap) -> impl IntoResponse {
let name = params
.get("name")
.expect("`name` will be there if route was matched")
.to_string();
async fn greet(extract::Path(name): extract::Path<String>) -> impl IntoResponse {
let template = HelloTemplate { name };
HtmlTemplate(template)
}
+3 -6
View File
@@ -14,7 +14,7 @@
//! ```
use axum::{
extract::{Extension, Json, Query, UrlParams},
extract::{Extension, Json, Path, Query},
prelude::*,
response::IntoResponse,
service::ServiceExt,
@@ -129,7 +129,7 @@ struct UpdateTodo {
}
async fn todos_update(
UrlParams((id,)): UrlParams<(Uuid,)>,
Path(id): Path<Uuid>,
Json(input): Json<UpdateTodo>,
Extension(db): Extension<Db>,
) -> Result<impl IntoResponse, StatusCode> {
@@ -153,10 +153,7 @@ async fn todos_update(
Ok(response::Json(todo))
}
async fn todos_delete(
UrlParams((id,)): UrlParams<(Uuid,)>,
Extension(db): Extension<Db>,
) -> impl IntoResponse {
async fn todos_delete(Path(id): Path<Uuid>, Extension(db): Extension<Db>) -> impl IntoResponse {
if db.write().unwrap().remove(&id).is_some() {
StatusCode::NO_CONTENT
} else {
+3 -2
View File
@@ -12,6 +12,7 @@ use axum::{
};
use http::Response;
use http::StatusCode;
use std::collections::HashMap;
use std::net::SocketAddr;
#[tokio::main]
@@ -53,7 +54,7 @@ where
type Rejection = Response<Body>;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = extract::UrlParamsMap::from_request(req)
let params = extract::Path::<HashMap<String, String>>::from_request(req)
.await
.map_err(IntoResponse::into_response)?;
@@ -61,7 +62,7 @@ where
.get("version")
.ok_or_else(|| (StatusCode::NOT_FOUND, "version param missing").into_response())?;
match version {
match version.as_str() {
"v1" => Ok(Version::V1),
"v2" => Ok(Version::V2),
"v3" => Ok(Version::V3),