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,22 +1,15 @@
use actix_web::{
HttpResponse,
body::BoxBody,
dev::{HttpServiceFactory, ServiceResponse},
http::{
Method, StatusCode,
header::{self, HeaderName, HeaderValue},
},
middleware::{ErrorHandlerResponse, ErrorHandlers},
web::Data,
};
use crate::address_object::resource::AddressObjectResourceService;
use crate::addressbook::resource::AddressbookResourceService;
use axum::{Extension, Router};
use derive_more::Constructor;
pub use error::Error;
use principal::PrincipalResourceService;
use rustical_dav::resource::{PrincipalUri, ResourceService};
use rustical_dav::resources::RootResourceService;
use rustical_store::auth::middleware::AuthenticationLayer;
use rustical_store::{
AddressbookStore, SubscriptionStore,
auth::{AuthenticationMiddleware, AuthenticationProvider, User},
auth::{AuthenticationProvider, User},
};
use std::sync::Arc;
@@ -34,51 +27,33 @@ impl PrincipalUri for CardDavPrincipalUri {
}
}
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
/// methods of a resource
fn options_handler() -> ErrorHandlers<BoxBody> {
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
Ok(ErrorHandlerResponse::Response(
if res.request().method() == Method::OPTIONS {
let mut response = HttpResponse::Ok();
response.insert_header((
HeaderName::from_static("dav"),
// https://datatracker.ietf.org/doc/html/rfc4918#section-18
HeaderValue::from_static(
"1, 3, access-control, addressbook, extended-mkcol, webdav-push",
),
));
if let Some(allow) = res.headers().get(header::ALLOW) {
response.insert_header((header::ALLOW, allow.to_owned()));
}
ServiceResponse::new(res.into_parts().0, response.finish()).map_into_right_body()
} else {
res.map_into_left_body()
},
))
})
}
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
pub fn carddav_router<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
prefix: &'static str,
auth_provider: Arc<AP>,
store: Arc<A>,
subscription_store: Arc<S>,
) -> impl HttpServiceFactory {
RootResourceService::<_, User, CardDavPrincipalUri>::new(
PrincipalResourceService::<_, _, S>::new(
store.clone(),
auth_provider.clone(),
subscription_store.clone(),
),
)
.actix_scope()
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::new(CardDavPrincipalUri::new(
format!("{prefix}/principal").leak(),
)))
// TODO: Add endpoint to delete subscriptions
) -> Router {
let principal_service = PrincipalResourceService::new(
store.clone(),
auth_provider.clone(),
subscription_store.clone(),
);
Router::new()
.route_service(
"/",
RootResourceService::<_, User, CardDavPrincipalUri>::new(principal_service.clone())
.axum_service(),
)
.route_service("/principal/{principal}", principal_service.axum_service())
.route_service(
"/principal/{principal}/{addressbook_id}",
AddressbookResourceService::new(store.clone(), subscription_store.clone())
.axum_service(),
)
.route_service(
"/principal/{principal}/{addressbook_id}/{object_id}",
AddressObjectResourceService::new(store.clone()).axum_service(),
)
.layer(AuthenticationLayer::new(auth_provider))
.layer(Extension(CardDavPrincipalUri(prefix)))
}