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,71 +1,70 @@
use super::resource::CalendarObjectPathComponents;
use crate::Error;
use crate::calendar_object::resource::CalendarObjectResourceService;
use crate::error::Precondition;
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 headers::{ContentType, ETag, HeaderMapExt, IfNoneMatch};
use http::StatusCode;
use rustical_ical::CalendarObject;
use rustical_store::CalendarStore;
use rustical_store::auth::User;
use std::str::FromStr;
use tracing::instrument;
use tracing_actix_web::RootSpan;
use super::resource::CalendarObjectPathComponents;
#[instrument(parent = root_span.id(), skip(store, root_span))]
#[instrument(skip(cal_store))]
pub async fn get_event<C: CalendarStore>(
path: Path<CalendarObjectPathComponents>,
store: Data<C>,
user: User,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let CalendarObjectPathComponents {
Path(CalendarObjectPathComponents {
principal,
calendar_id,
object_id,
} = path.into_inner();
}): Path<CalendarObjectPathComponents>,
State(CalendarObjectResourceService { cal_store }): State<CalendarObjectResourceService<C>>,
user: User,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Ok(HttpResponse::Unauthorized().body(""));
return Err(crate::Error::Unauthorized);
}
let calendar = store.get_calendar(&principal, &calendar_id).await?;
let calendar = cal_store.get_calendar(&principal, &calendar_id).await?;
if !user.is_principal(&calendar.principal) {
return Ok(HttpResponse::Unauthorized().body(""));
return Err(crate::Error::Unauthorized);
}
let event = store
let event = cal_store
.get_object(&principal, &calendar_id, &object_id)
.await?;
Ok(HttpResponse::Ok()
.insert_header(("ETag", event.get_etag()))
.insert_header(("Content-Type", "text/calendar"))
.body(event.get_ics().to_owned()))
let mut resp = Response::builder().status(StatusCode::OK);
let hdrs = resp.headers_mut().unwrap();
hdrs.typed_insert(ETag::from_str(&event.get_etag()).unwrap());
hdrs.typed_insert(ContentType::from_str("text/calendar").unwrap());
Ok(resp.body(Body::new(event.get_ics().to_owned())).unwrap())
}
#[instrument(parent = root_span.id(), skip(store, req, root_span))]
#[instrument(skip(cal_store))]
pub async fn put_event<C: CalendarStore>(
path: Path<CalendarObjectPathComponents>,
store: Data<C>,
body: String,
user: User,
req: HttpRequest,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let CalendarObjectPathComponents {
Path(CalendarObjectPathComponents {
principal,
calendar_id,
object_id,
} = path.into_inner();
}): Path<CalendarObjectPathComponents>,
State(CalendarObjectResourceService { cal_store }): State<CalendarObjectResourceService<C>>,
user: User,
if_none_match: Option<TypedHeader<IfNoneMatch>>,
body: String,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Ok(HttpResponse::Unauthorized().finish());
return Err(crate::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 = match CalendarObject::from_ics(object_id, body) {
Ok(obj) => obj,
@@ -73,9 +72,9 @@ pub async fn put_event<C: CalendarStore>(
return Err(Error::PreconditionFailed(Precondition::ValidCalendarData));
}
};
store
cal_store
.put_object(principal, calendar_id, object, overwrite)
.await?;
Ok(HttpResponse::Created().finish())
Ok(StatusCode::CREATED.into_response())
}

View File

@@ -1,22 +1,35 @@
use super::methods::{get_event, put_event};
use crate::{CalDavPrincipalUri, Error};
use actix_web::web;
// use super::methods::{get_event, put_event};
use crate::{
CalDavPrincipalUri, Error,
calendar_object::methods::{get_event, put_event},
};
use async_trait::async_trait;
use axum::{extract::Request, handler::Handler, response::Response};
use derive_more::derive::{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::{CalendarObject, UtcDateTime};
use rustical_store::{CalendarStore, auth::User};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use serde::Deserialize;
use std::sync::Arc;
use serde::{Deserialize, Deserializer};
use std::{convert::Infallible, sync::Arc};
use tower::Service;
pub struct CalendarObjectResourceService<C: CalendarStore> {
cal_store: Arc<C>,
pub(crate) cal_store: Arc<C>,
}
impl<C: CalendarStore> Clone for CalendarObjectResourceService<C> {
fn clone(&self) -> Self {
Self {
cal_store: self.cal_store.clone(),
}
}
}
impl<C: CalendarStore> CalendarObjectResourceService<C> {
@@ -130,10 +143,23 @@ impl Resource for CalendarObjectResource {
}
}
fn deserialize_ics_name<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let name: String = Deserialize::deserialize(deserializer)?;
if let Some(object_id) = name.strip_suffix(".ics") {
Ok(object_id.to_owned())
} else {
Err(serde::de::Error::custom("Missing .ics extension"))
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct CalendarObjectPathComponents {
pub principal: String,
pub calendar_id: String,
#[serde(deserialize_with = "deserialize_ics_name")]
pub object_id: String,
}
@@ -180,12 +206,19 @@ impl<C: CalendarStore> ResourceService for CalendarObjectResourceService<C> {
.await?;
Ok(())
}
}
fn actix_scope(self) -> actix_web::Scope {
web::scope("/{object_id}.ics").service(
self.actix_resource()
.get(get_event::<C>)
.put(put_event::<C>),
)
impl<C: CalendarStore> AxumMethods for CalendarObjectResourceService<C> {
fn get() -> Option<fn(Self, Request) -> BoxFuture<'static, Result<Response, Infallible>>> {
Some(|state, req| {
let mut service = Handler::with_state(get_event::<C>, 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_event::<C>, state);
Box::pin(Service::call(&mut service, req))
})
}
}