Lots of refactoring around routing

This commit is contained in:
Lennart
2025-06-02 19:41:30 +02:00
parent 08c4bd4289
commit b7c24fe2f0
13 changed files with 245 additions and 212 deletions

View File

@@ -2,10 +2,10 @@ use super::methods::mkcalendar::route_mkcalendar;
use super::methods::post::route_post;
use super::methods::report::route_report_calendar;
use super::prop::{SupportedCalendarComponentSet, SupportedCalendarData, SupportedReportSet};
use crate::calendar_object::resource::CalendarObjectResource;
use crate::calendar_object::resource::{CalendarObjectResource, CalendarObjectResourceService};
use crate::{CalDavPrincipalUri, Error};
use actix_web::http::Method;
use actix_web::web;
use actix_web::web::{self, Data};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use derive_more::derive::{From, Into};
@@ -21,7 +21,6 @@ use rustical_store::auth::User;
use rustical_store::{Calendar, CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants};
use rustical_xml::{XmlDeserialize, XmlSerialize};
use std::marker::PhantomData;
use std::str::FromStr;
use std::sync::Arc;
@@ -311,14 +310,14 @@ impl Resource for CalendarResource {
pub struct CalendarResourceService<C: CalendarStore, S: SubscriptionStore> {
cal_store: Arc<C>,
__phantom_sub: PhantomData<S>,
sub_store: Arc<S>,
}
impl<C: CalendarStore, S: SubscriptionStore> CalendarResourceService<C, S> {
pub fn new(cal_store: Arc<C>) -> Self {
pub fn new(cal_store: Arc<C>, sub_store: Arc<S>) -> Self {
Self {
cal_store,
__phantom_sub: PhantomData,
sub_store,
}
}
}
@@ -386,13 +385,17 @@ impl<C: CalendarStore, S: SubscriptionStore> ResourceService for CalendarResourc
Ok(())
}
#[inline]
fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource {
fn actix_scope(self) -> actix_web::Scope {
let report_method = web::method(Method::from_str("REPORT").unwrap());
let mkcalendar_method = web::method(Method::from_str("MKCALENDAR").unwrap());
res.route(report_method.to(route_report_calendar::<C>))
.route(mkcalendar_method.to(route_mkcalendar::<C>))
.post(route_post::<C, S>)
web::scope("/{calendar_id}")
.app_data(Data::from(self.sub_store.clone()))
.service(CalendarObjectResourceService::new(self.cal_store.clone()).actix_scope())
.service(
self.actix_resource()
.route(report_method.to(route_report_calendar::<C>))
.route(mkcalendar_method.to(route_mkcalendar::<C>))
.post(route_post::<C, S>),
)
}
}

View File

@@ -1,5 +1,6 @@
use super::methods::{get_event, put_event};
use crate::{CalDavPrincipalUri, Error};
use actix_web::web;
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use rustical_dav::{
@@ -149,8 +150,11 @@ impl<C: CalendarStore> ResourceService for CalendarObjectResourceService<C> {
Ok(())
}
#[inline]
fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource {
res.get(get_event::<C>).put(put_event::<C>)
fn actix_scope(self) -> actix_web::Scope {
web::scope("/{object_id}.ics").service(
self.actix_resource()
.get(get_event::<C>)
.put(put_event::<C>),
)
}
}

View File

@@ -1,12 +1,13 @@
use crate::calendar::resource::CalendarResource;
use crate::calendar::resource::{CalendarResource, CalendarResourceService};
use crate::{CalDavPrincipalUri, Error};
use actix_web::web;
use async_trait::async_trait;
use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{Resourcetype, ResourcetypeInner};
use rustical_store::CalendarStore;
use rustical_store::auth::User;
use rustical_store::{CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
@@ -60,18 +61,24 @@ impl Resource for CalendarSetResource {
}
}
pub struct CalendarSetResourceService<C: CalendarStore> {
pub struct CalendarSetResourceService<C: CalendarStore, S: SubscriptionStore> {
name: &'static str,
cal_store: Arc<C>,
sub_store: Arc<S>,
}
impl<C: CalendarStore> CalendarSetResourceService<C> {
pub fn new(cal_store: Arc<C>) -> Self {
Self { cal_store }
impl<C: CalendarStore, S: SubscriptionStore> CalendarSetResourceService<C, S> {
pub fn new(name: &'static str, cal_store: Arc<C>, sub_store: Arc<S>) -> Self {
Self {
name,
cal_store,
sub_store,
}
}
}
#[async_trait(?Send)]
impl<C: CalendarStore> ResourceService for CalendarSetResourceService<C> {
impl<C: CalendarStore, S: SubscriptionStore> ResourceService for CalendarSetResourceService<C, S> {
type PathComponents = (String,);
type MemberType = CalendarResource;
type Resource = CalendarSetResource;
@@ -107,4 +114,16 @@ impl<C: CalendarStore> ResourceService for CalendarSetResourceService<C> {
})
.collect())
}
fn actix_scope(self) -> actix_web::Scope {
web::scope(&format!("/{}", self.name))
.service(
CalendarResourceService::<_, S>::new(
self.cal_store.clone(),
self.sub_store.clone(),
)
.actix_scope(),
)
.service(self.actix_resource())
}
}

View File

@@ -4,13 +4,10 @@ use actix_web::dev::{HttpServiceFactory, ServiceResponse};
use actix_web::http::header::{self, HeaderName, HeaderValue};
use actix_web::http::{Method, StatusCode};
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::web::{self, Data};
use calendar::resource::CalendarResourceService;
use calendar_object::resource::CalendarObjectResourceService;
use calendar_set::CalendarSetResourceService;
use actix_web::web::Data;
use derive_more::Constructor;
use principal::{PrincipalResource, PrincipalResourceService};
use rustical_dav::resource::{PrincipalUri, ResourceService, ResourceServiceRoute};
use principal::PrincipalResourceService;
use rustical_dav::resource::{PrincipalUri, ResourceService};
use rustical_dav::resources::RootResourceService;
use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider, User};
use rustical_store::{AddressbookStore, CalendarStore, ContactBirthdayStore, SubscriptionStore};
@@ -75,71 +72,19 @@ pub fn caldav_service<
) -> impl HttpServiceFactory {
let birthday_store = Arc::new(ContactBirthdayStore::new(addr_store));
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::from(birthday_store.clone()))
.app_data(Data::from(subscription_store))
.app_data(Data::new(CalDavPrincipalUri::new(
format!("{prefix}/principal").leak(),
)))
.service(
RootResourceService::<PrincipalResource, User, CalDavPrincipalUri>::default()
.actix_resource(),
)
.service(
web::scope("/principal").service(
web::scope("/{principal}")
.service(
PrincipalResourceService {
auth_provider,
home_set: &[("calendar", false), ("birthdays", true)],
}
.actix_resource(),
)
.service(
web::scope("/calendar")
.service(
CalendarSetResourceService::new(store.clone()).actix_resource(),
)
.service(
web::scope("/{calendar_id}")
.service(ResourceServiceRoute(
CalendarResourceService::<_, S>::new(store.clone()),
))
.service(
web::scope("/{object_id}.ics").service(
CalendarObjectResourceService::new(store.clone())
.actix_resource(),
),
),
),
)
.service(
web::scope("/birthdays")
.service(
CalendarSetResourceService::new(birthday_store.clone())
.actix_resource(),
)
.service(
web::scope("/{calendar_id}")
.service(ResourceServiceRoute(
CalendarResourceService::<_, S>::new(
birthday_store.clone(),
),
))
.service(
web::scope("/{object_id}.ics").service(
CalendarObjectResourceService::new(
birthday_store.clone(),
)
.actix_resource(),
),
),
),
),
),
)
.service(subscription_resource::<S>())
RootResourceService::<_, User, CalDavPrincipalUri>::new(PrincipalResourceService {
auth_provider: auth_provider.clone(),
sub_store: subscription_store.clone(),
birthday_store: birthday_store.clone(),
cal_store: store.clone(),
})
.actix_scope()
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::from(birthday_store.clone()))
.app_data(Data::new(CalDavPrincipalUri::new(
format!("{prefix}/principal").leak(),
)))
.service(subscription_resource(subscription_store))
}

View File

@@ -1,5 +1,6 @@
use crate::calendar_set::CalendarSetResource;
use crate::calendar_set::{CalendarSetResource, CalendarSetResourceService};
use crate::{CalDavPrincipalUri, Error};
use actix_web::web;
use async_trait::async_trait;
use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
use rustical_dav::privileges::UserPrivilegeSet;
@@ -7,13 +8,14 @@ use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_store::auth::user::PrincipalType;
use rustical_store::auth::{AuthenticationProvider, User};
use rustical_store::{CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
#[derive(Clone)]
pub struct PrincipalResource {
principal: User,
home_set: &'static [(&'static str, bool)],
home_set: &'static [&'static str],
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
@@ -72,7 +74,7 @@ impl Resource for PrincipalResource {
.into_iter()
.map(|principal| puri.principal_uri(principal))
.flat_map(|principal_url| {
self.home_set.iter().map(move |&(home_name, _read_only)| {
self.home_set.iter().map(move |&home_name| {
HrefElement::new(format!("{}/{}", &principal_url, home_name))
})
})
@@ -117,13 +119,36 @@ impl Resource for PrincipalResource {
}
}
pub struct PrincipalResourceService<AP: AuthenticationProvider> {
pub auth_provider: Arc<AP>,
pub home_set: &'static [(&'static str, bool)],
#[derive(Debug)]
pub struct PrincipalResourceService<
AP: AuthenticationProvider,
S: SubscriptionStore,
CS: CalendarStore,
BS: CalendarStore,
> {
pub(crate) auth_provider: Arc<AP>,
pub(crate) sub_store: Arc<S>,
pub(crate) cal_store: Arc<CS>,
pub(crate) birthday_store: Arc<BS>,
}
impl<AP: AuthenticationProvider, S: SubscriptionStore, CS: CalendarStore, BS: CalendarStore> Clone
for PrincipalResourceService<AP, S, CS, BS>
{
fn clone(&self) -> Self {
Self {
auth_provider: self.auth_provider.clone(),
sub_store: self.sub_store.clone(),
cal_store: self.cal_store.clone(),
birthday_store: self.birthday_store.clone(),
}
}
}
#[async_trait(?Send)]
impl<AP: AuthenticationProvider> ResourceService for PrincipalResourceService<AP> {
impl<AP: AuthenticationProvider, S: SubscriptionStore, CS: CalendarStore, BS: CalendarStore>
ResourceService for PrincipalResourceService<AP, S, CS, BS>
{
type PathComponents = (String,);
type MemberType = CalendarSetResource;
type Resource = PrincipalResource;
@@ -142,7 +167,7 @@ impl<AP: AuthenticationProvider> ResourceService for PrincipalResourceService<AP
.ok_or(crate::Error::NotFound)?;
Ok(PrincipalResource {
principal: user,
home_set: self.home_set,
home_set: &["calendar", "birthdays"],
})
}
@@ -150,18 +175,42 @@ impl<AP: AuthenticationProvider> ResourceService for PrincipalResourceService<AP
&self,
(principal,): &Self::PathComponents,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(self
.home_set
.iter()
.map(|&(set_name, read_only)| {
(
set_name.to_string(),
CalendarSetResource {
principal: principal.to_owned(),
read_only,
},
Ok(vec![
(
"calendar".to_owned(),
CalendarSetResource {
principal: principal.to_owned(),
read_only: false,
},
),
(
"birthdays".to_owned(),
CalendarSetResource {
principal: principal.to_owned(),
read_only: true,
},
),
])
}
fn actix_scope(self) -> actix_web::Scope {
web::scope("/principal/{principal}")
.service(
CalendarSetResourceService::<_, S>::new(
"calendar",
self.cal_store.clone(),
self.sub_store.clone(),
)
})
.collect())
.actix_scope(),
)
.service(
CalendarSetResourceService::<_, S>::new(
"birthdays",
self.birthday_store.clone(),
self.sub_store.clone(),
)
.actix_scope(),
)
.service(self.actix_resource())
}
}

View File

@@ -1,6 +1,8 @@
use std::sync::Arc;
use actix_web::{
web::{self, Data, Path},
HttpResponse,
web::{self, Data, Path},
};
use rustical_dav::xml::multistatus::PropstatElement;
use rustical_store::SubscriptionStore;
@@ -17,8 +19,9 @@ async fn handle_delete<S: SubscriptionStore>(
Ok(HttpResponse::NoContent().body("Unregistered"))
}
pub fn subscription_resource<S: SubscriptionStore>() -> actix_web::Resource {
pub fn subscription_resource<S: SubscriptionStore>(sub_store: Arc<S>) -> actix_web::Resource {
web::resource("/subscription/{id}")
.app_data(Data::from(sub_store))
.name("subscription")
.delete(handle_delete::<S>)
}