From 9e724d3903caf645820580c144de0e5dcd5580b3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 12 May 2023 16:43:28 +0200 Subject: [PATCH] got manager being shared properly into render context --- src/domain/checker.rs | 2 +- src/domain/config.rs | 2 +- src/domain/manager.rs | 2 +- src/origin/store.rs | 2 +- src/service.rs | 61 +++++++++++++++++++++++++++++-------------- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/domain/checker.rs b/src/domain/checker.rs index c0f0014..46a0802 100644 --- a/src/domain/checker.rs +++ b/src/domain/checker.rs @@ -36,7 +36,7 @@ pub enum CheckDomainError { } #[automock] -pub trait Checker { +pub trait Checker: std::marker::Send + std::marker::Sync { fn check_domain( &self, domain: &domain::Name, diff --git a/src/domain/config.rs b/src/domain/config.rs index e89fe5b..fe76070 100644 --- a/src/domain/config.rs +++ b/src/domain/config.rs @@ -39,7 +39,7 @@ pub enum SetError { } #[mockall::automock] -pub trait Store { +pub trait Store: std::marker::Send + std::marker::Sync { fn get(&self, domain: &domain::Name) -> Result; fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>; } diff --git a/src/domain/manager.rs b/src/domain/manager.rs index 3690b97..81f78e3 100644 --- a/src/domain/manager.rs +++ b/src/domain/manager.rs @@ -116,7 +116,7 @@ impl From for SyncWithConfigError { } #[mockall::automock(type Origin=origin::MockOrigin;)] -pub trait Manager { +pub trait Manager: std::marker::Send + std::marker::Sync { type Origin<'mgr>: origin::Origin + 'mgr where Self: 'mgr; diff --git a/src/origin/store.rs b/src/origin/store.rs index 23e3e5b..aa09229 100644 --- a/src/origin/store.rs +++ b/src/origin/store.rs @@ -46,7 +46,7 @@ pub type AllDescrsResult = Result; type AllDescrsIter=Vec>; )] /// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr. -pub trait Store { +pub trait Store: std::marker::Send + std::marker::Sync { type Origin<'store>: origin::Origin + 'store where Self: 'store; diff --git a/src/service.rs b/src/service.rs index 482ff37..d62057c 100644 --- a/src/service.rs +++ b/src/service.rs @@ -16,7 +16,11 @@ pub mod http_tpl; type Handlebars<'a> = sync::Arc>; -struct RenderContext<'a> { +struct RenderContext<'a, DM> +where + DM: domain::manager::Manager, +{ + domain_manager: sync::Arc, handlebars: Handlebars<'a>, query_args: HashMap, } @@ -40,9 +44,14 @@ where warp::reply::with_header(reply, "Content-Type", content_type) } -fn render_page<'a, T>(render_ctx: RenderContext<'a>, name: String, data: T) -> impl warp::Reply +fn render_page<'a, T, DM>( + render_ctx: RenderContext<'a, DM>, + name: String, + data: T, +) -> impl warp::Reply where T: Serialize, + DM: domain::manager::Manager, { #[derive(Serialize)] struct Presenter { @@ -60,16 +69,22 @@ where render(render_ctx.handlebars, "/base.html", presenter) } -pub fn new( - _manager: impl domain::manager::Manager, +pub fn new( + manager: DM, ) -> Result< - impl warp::Filter + Clone, + impl warp::Filter + Clone + 'static, Box, -> { +> +where + DM: domain::manager::Manager + 'static, +{ + let manager = sync::Arc::new(manager); + let hbs = sync::Arc::new(self::http_tpl::get()?); let with_render_ctx = warp::any() .and(warp::query::>()) .map(move |query_args: HashMap| RenderContext { + domain_manager: manager.clone(), handlebars: hbs.clone(), query_args, }); @@ -79,7 +94,7 @@ pub fn new( .and(warp::path("static")) .and(warp::path::full()) .map( - |render_ctx: RenderContext<'_>, full: warp::path::FullPath| { + |render_ctx: RenderContext<'_, DM>, full: warp::path::FullPath| { render(render_ctx.handlebars, full.as_str(), ()) }, ); @@ -87,31 +102,37 @@ pub fn new( let index = warp::get() .and(with_render_ctx.clone()) .and(warp::path::end()) - .map(|render_ctx: RenderContext<'_>| { + .map(|render_ctx: RenderContext<'_, DM>| { render_page(render_ctx, String::from("/index.html"), ()) }); #[derive(Deserialize)] struct DomainGetNewRequest { - domain: String, + domain: domain::Name, } let domain_get = warp::get() .and(with_render_ctx.clone()) .and(warp::path("domain.html")) .and(warp::query::()) - .map(|render_ctx: RenderContext<'_>, req: DomainGetNewRequest| { - #[derive(Serialize)] - struct DomainGetNewResponse { - domain: String, - } + .map( + move |render_ctx: RenderContext<'_, DM>, req: DomainGetNewRequest| { + #[derive(Serialize)] + struct DomainGetNewResponse { + domain: domain::Name, + } - render_page( - render_ctx, - String::from("/domain_get_new.html"), - DomainGetNewResponse { domain: req.domain }, - ) - }); + match render_ctx.domain_manager.get_config(&req.domain) { + Ok(_config) => panic!("TODO"), + Err(domain::manager::GetConfigError::NotFound) => render_page( + render_ctx, + String::from("/domain_get_new.html"), + DomainGetNewResponse { domain: req.domain }, + ), + Err(_) => panic!("TODO"), + } + }, + ); Ok(static_dir.or(index).or(domain_get)) }