Checkpoint: Migration to axum

This commit is contained in:
Lennart
2025-06-08 14:10:12 +02:00
parent 790c657b08
commit 95889e3df1
60 changed files with 1476 additions and 2205 deletions

View File

@@ -1,37 +1,37 @@
use std::str::FromStr;
use super::resource::AddressObjectPathComponents;
use crate::Error;
use crate::address_object::resource::AddressObjectResourceService;
use crate::addressbook::resource::AddressbookResource;
use actix_web::HttpRequest;
use actix_web::HttpResponse;
use actix_web::http::header;
use actix_web::http::header::HeaderValue;
use actix_web::web::{Data, Path};
use axum::body::Body;
use axum::extract::{Path, State};
use axum::response::{IntoResponse, Response};
use axum_extra::TypedHeader;
use axum_extra::headers::{ContentType, ETag, HeaderMapExt, IfNoneMatch};
use http::StatusCode;
use rustical_dav::privileges::UserPrivilege;
use rustical_dav::resource::Resource;
use rustical_ical::AddressObject;
use rustical_store::AddressbookStore;
use rustical_store::auth::User;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(store, root_span))]
#[instrument(skip(addr_store))]
pub async fn get_object<AS: AddressbookStore>(
path: Path<AddressObjectPathComponents>,
store: Data<AS>,
user: User,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let AddressObjectPathComponents {
Path(AddressObjectPathComponents {
principal,
addressbook_id,
object_id,
} = path.into_inner();
}): Path<AddressObjectPathComponents>,
State(AddressObjectResourceService { addr_store }): State<AddressObjectResourceService<AS>>,
user: User,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Err(Error::Unauthorized);
}
let addressbook = store
let addressbook = addr_store
.get_addressbook(&principal, &addressbook_id, false)
.await?;
let addressbook_resource = AddressbookResource(addressbook);
@@ -42,42 +42,43 @@ pub async fn get_object<AS: AddressbookStore>(
return Err(Error::Unauthorized);
}
let object = store
let object = addr_store
.get_object(&principal, &addressbook_id, &object_id, false)
.await?;
Ok(HttpResponse::Ok()
.insert_header(("ETag", object.get_etag()))
.insert_header(("Content-Type", "text/vcard"))
.body(object.get_vcf().to_owned()))
let mut resp = Response::builder().status(StatusCode::OK);
let hdrs = resp.headers_mut().unwrap();
hdrs.typed_insert(ETag::from_str(&object.get_etag()).unwrap());
hdrs.typed_insert(ContentType::from_str("text/vcard").unwrap());
Ok(resp.body(Body::new(object.get_vcf().to_owned())).unwrap())
}
#[instrument(parent = root_span.id(), skip(store, req, root_span))]
#[instrument(skip(addr_store, body))]
pub async fn put_object<AS: AddressbookStore>(
path: Path<AddressObjectPathComponents>,
store: Data<AS>,
body: String,
user: User,
req: HttpRequest,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let AddressObjectPathComponents {
Path(AddressObjectPathComponents {
principal,
addressbook_id,
object_id,
} = path.into_inner();
}): Path<AddressObjectPathComponents>,
State(AddressObjectResourceService { addr_store }): State<AddressObjectResourceService<AS>>,
user: User,
if_none_match: Option<TypedHeader<IfNoneMatch>>,
body: String,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Err(Error::Unauthorized);
}
let overwrite =
Some(&HeaderValue::from_static("*")) != req.headers().get(header::IF_NONE_MATCH);
let overwrite = if let Some(TypedHeader(if_none_match)) = if_none_match {
if_none_match == IfNoneMatch::any()
} else {
true
};
let object = AddressObject::from_vcf(object_id, body)?;
store
addr_store
.put_object(principal, addressbook_id, object, overwrite)
.await?;
Ok(HttpResponse::Created().finish())
Ok(StatusCode::CREATED.into_response())
}

View File

@@ -1,24 +1,34 @@
use crate::{CardDavPrincipalUri, Error};
use actix_web::web;
use async_trait::async_trait;
use axum::{extract::Request, handler::Handler, response::Response};
use derive_more::derive::{Constructor, From, Into};
use futures_util::future::BoxFuture;
use rustical_dav::{
extensions::{CommonPropertiesExtension, CommonPropertiesProp},
privileges::UserPrivilegeSet,
resource::{PrincipalUri, Resource, ResourceService},
resource::{AxumMethods, PrincipalUri, Resource, ResourceService},
xml::Resourcetype,
};
use rustical_ical::AddressObject;
use rustical_store::{AddressbookStore, auth::User};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use serde::Deserialize;
use std::sync::Arc;
use std::{convert::Infallible, sync::Arc};
use tower::Service;
use super::methods::{get_object, put_object};
#[derive(Constructor)]
pub struct AddressObjectResourceService<AS: AddressbookStore> {
addr_store: Arc<AS>,
pub(crate) addr_store: Arc<AS>,
}
impl<AS: AddressbookStore> Clone for AddressObjectResourceService<AS> {
fn clone(&self) -> Self {
Self {
addr_store: self.addr_store.clone(),
}
}
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
@@ -148,13 +158,20 @@ impl<AS: AddressbookStore> ResourceService for AddressObjectResourceService<AS>
.await?;
Ok(())
}
}
#[inline]
fn actix_scope(self) -> actix_web::Scope {
web::scope("/{object_id}.vcf").service(
self.actix_resource()
.get(get_object::<AS>)
.put(put_object::<AS>),
)
impl<AS: AddressbookStore> AxumMethods for AddressObjectResourceService<AS> {
fn get() -> Option<fn(Self, Request) -> BoxFuture<'static, Result<Response, Infallible>>> {
Some(|state, req| {
let mut service = Handler::with_state(get_object::<AS>, state);
Box::pin(Service::call(&mut service, req))
})
}
fn put() -> Option<fn(Self, Request) -> BoxFuture<'static, Result<Response, Infallible>>> {
Some(|state, req| {
let mut service = Handler::with_state(put_object::<AS>, state);
Box::pin(Service::call(&mut service, req))
})
}
}