Work on axum support

This commit is contained in:
Lennart
2025-06-07 20:17:50 +02:00
parent 57832116aa
commit 790c657b08
38 changed files with 582 additions and 64 deletions

View File

@@ -2,9 +2,17 @@ use crate::Error;
use crate::privileges::UserPrivilege;
use crate::resource::Resource;
use crate::resource::ResourceService;
#[cfg(feature = "axum")]
use axum::extract::{Extension, Path, State};
#[cfg(feature = "axum")]
use axum_extra::TypedHeader;
use headers::Header;
use headers::{HeaderValue, IfMatch, IfNoneMatch};
#[cfg(feature = "axum")]
use http::HeaderMap;
use itertools::Itertools;
#[cfg(feature = "axum")]
use std::sync::Arc;
use tracing::instrument;
#[cfg(feature = "actix")]
@@ -27,12 +35,12 @@ pub async fn actix_route_delete<R: ResourceService>(
// while actix-web still uses http==0.2
let if_match = req
.headers()
.get_all(http::header::IF_MATCH)
.get_all(http_02::header::IF_MATCH)
.map(|val_02| HeaderValue::from_bytes(val_02.as_bytes()).unwrap())
.collect_vec();
let if_none_match = req
.headers()
.get_all(http::header::IF_NONE_MATCH)
.get_all(http_02::header::IF_NONE_MATCH)
.map(|val_02| HeaderValue::from_bytes(val_02.as_bytes()).unwrap())
.collect_vec();
@@ -49,7 +57,7 @@ pub async fn actix_route_delete<R: ResourceService>(
route_delete(
&path.into_inner(),
principal,
&principal,
resource_service.as_ref(),
no_trash,
if_match,
@@ -60,9 +68,33 @@ pub async fn actix_route_delete<R: ResourceService>(
Ok(actix_web::HttpResponse::Ok().body(""))
}
#[cfg(feature = "axum")]
pub(crate) async fn axum_route_delete<R: ResourceService>(
Path(path): Path<R::PathComponents>,
State(resource_service): State<Arc<R>>,
Extension(principal): Extension<R::Principal>,
if_match: Option<TypedHeader<IfMatch>>,
if_none_match: Option<TypedHeader<IfNoneMatch>>,
header_map: HeaderMap,
) -> Result<(), R::Error> {
let no_trash = header_map
.get("X-No-Trashbin")
.map(|val| matches!(val.to_str(), Ok("1")))
.unwrap_or(false);
route_delete(
&path,
&principal,
resource_service.as_ref(),
no_trash,
if_match.map(|hdr| hdr.0),
if_none_match.map(|hdr| hdr.0),
)
.await
}
pub async fn route_delete<R: ResourceService>(
path_components: &R::PathComponents,
principal: R::Principal,
principal: &R::Principal,
resource_service: &R,
no_trash: bool,
if_match: Option<IfMatch>,
@@ -70,7 +102,7 @@ pub async fn route_delete<R: ResourceService>(
) -> Result<(), R::Error> {
let resource = resource_service.get_resource(path_components).await?;
let privileges = resource.get_user_privileges(&principal)?;
let privileges = resource.get_user_privileges(principal)?;
if !privileges.has(&UserPrivilege::Write) {
return Err(Error::Unauthorized.into());
}