diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..6eb2a21 --- /dev/null +++ b/src/app.rs @@ -0,0 +1,36 @@ +use std::sync::Arc; + +use actix_web::body::MessageBody; +use actix_web::dev::{ServiceFactory, ServiceRequest, ServiceResponse}; +use actix_web::middleware::{Logger, NormalizePath}; +use actix_web::{web, App}; +use rustical_api::configure_api; +use rustical_auth::CheckAuthentication; +use rustical_caldav::{configure_dav, configure_well_known}; +use rustical_store::calendar::CalendarStore; +use tokio::sync::RwLock; + +pub fn make_app( + cal_store: Arc>, + auth: Arc, +) -> App< + impl ServiceFactory< + ServiceRequest, + Response = ServiceResponse, + Config = (), + InitError = (), + Error = actix_web::Error, + >, +> { + App::new() + .wrap(Logger::new("[%s] %r")) + .wrap(NormalizePath::trim()) + .service(web::scope("/caldav").configure(|cfg| { + configure_dav(cfg, "/caldav".to_string(), auth.clone(), cal_store.clone()) + })) + .service( + web::scope("/.well-known") + .configure(|cfg| configure_well_known(cfg, "/caldav".to_string())), + ) + .service(web::scope("/api").configure(|cfg| configure_api(cfg, cal_store.clone().into()))) +} diff --git a/src/main.rs b/src/main.rs index 7fa5cb9..e74fd4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,10 @@ use crate::config::Config; -use actix_web::middleware::{Logger, NormalizePath}; -use actix_web::{web, App, HttpServer}; +use actix_web::HttpServer; use anyhow::Result; +use app::make_app; use clap::Parser; use config::{CalendarStoreConfig, SqliteCalendarStoreConfig, TomlCalendarStoreConfig}; -use rustical_api::configure_api; use rustical_auth::AuthProvider; -use rustical_caldav::{configure_dav, configure_well_known}; use rustical_store::calendar::CalendarStore; use rustical_store::sqlite_store::SqliteCalendarStore; use rustical_store::toml_store::TomlCalendarStore; @@ -16,6 +14,7 @@ use std::fs; use std::sync::Arc; use tokio::sync::RwLock; +mod app; mod config; #[derive(Parser, Debug)] @@ -66,24 +65,10 @@ async fn main() -> Result<()> { let auth: Arc = Arc::new(config.auth.into()); - HttpServer::new(move || { - let cal_store = cal_store.clone(); - App::new() - .wrap(Logger::new("[%s] %r")) - .wrap(NormalizePath::trim()) - .service(web::scope("/caldav").configure(|cfg| { - configure_dav(cfg, "/caldav".to_string(), auth.clone(), cal_store.clone()) - })) - .service( - web::scope("/.well-known") - .configure(|cfg| configure_well_known(cfg, "/caldav".to_string())), - ) - .service( - web::scope("/api").configure(|cfg| configure_api(cfg, cal_store.clone().into())), - ) - }) - .bind((config.http.host, config.http.port))? - .run() - .await?; + HttpServer::new(move || make_app(cal_store.clone(), auth.clone())) + .bind((config.http.host, config.http.port))? + .run() + .await?; + Ok(()) }