got manager being shared properly into render context

This commit is contained in:
Brian Picciano 2023-05-12 16:43:28 +02:00
parent 7718735215
commit 9e724d3903
5 changed files with 45 additions and 24 deletions

View File

@ -36,7 +36,7 @@ pub enum CheckDomainError {
} }
#[automock] #[automock]
pub trait Checker { pub trait Checker: std::marker::Send + std::marker::Sync {
fn check_domain( fn check_domain(
&self, &self,
domain: &domain::Name, domain: &domain::Name,

View File

@ -39,7 +39,7 @@ pub enum SetError {
} }
#[mockall::automock] #[mockall::automock]
pub trait Store { pub trait Store: std::marker::Send + std::marker::Sync {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError>; fn get(&self, domain: &domain::Name) -> Result<Config, GetError>;
fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>; fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>;
} }

View File

@ -116,7 +116,7 @@ impl From<config::SetError> for SyncWithConfigError {
} }
#[mockall::automock(type Origin=origin::MockOrigin;)] #[mockall::automock(type Origin=origin::MockOrigin;)]
pub trait Manager { pub trait Manager: std::marker::Send + std::marker::Sync {
type Origin<'mgr>: origin::Origin + 'mgr type Origin<'mgr>: origin::Origin + 'mgr
where where
Self: 'mgr; Self: 'mgr;

View File

@ -46,7 +46,7 @@ pub type AllDescrsResult<T> = Result<T, AllDescrsError>;
type AllDescrsIter=Vec<AllDescrsResult<origin::Descr>>; type AllDescrsIter=Vec<AllDescrsResult<origin::Descr>>;
)] )]
/// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr. /// 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 type Origin<'store>: origin::Origin + 'store
where where
Self: 'store; Self: 'store;

View File

@ -16,7 +16,11 @@ pub mod http_tpl;
type Handlebars<'a> = sync::Arc<handlebars::Handlebars<'a>>; type Handlebars<'a> = sync::Arc<handlebars::Handlebars<'a>>;
struct RenderContext<'a> { struct RenderContext<'a, DM>
where
DM: domain::manager::Manager,
{
domain_manager: sync::Arc<DM>,
handlebars: Handlebars<'a>, handlebars: Handlebars<'a>,
query_args: HashMap<String, String>, query_args: HashMap<String, String>,
} }
@ -40,9 +44,14 @@ where
warp::reply::with_header(reply, "Content-Type", content_type) 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 where
T: Serialize, T: Serialize,
DM: domain::manager::Manager,
{ {
#[derive(Serialize)] #[derive(Serialize)]
struct Presenter<T> { struct Presenter<T> {
@ -60,16 +69,22 @@ where
render(render_ctx.handlebars, "/base.html", presenter) render(render_ctx.handlebars, "/base.html", presenter)
} }
pub fn new( pub fn new<DM>(
_manager: impl domain::manager::Manager, manager: DM,
) -> Result< ) -> Result<
impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone, impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone + 'static,
Box<dyn Error>, Box<dyn Error>,
> { >
where
DM: domain::manager::Manager + 'static,
{
let manager = sync::Arc::new(manager);
let hbs = sync::Arc::new(self::http_tpl::get()?); let hbs = sync::Arc::new(self::http_tpl::get()?);
let with_render_ctx = warp::any() let with_render_ctx = warp::any()
.and(warp::query::<HashMap<String, String>>()) .and(warp::query::<HashMap<String, String>>())
.map(move |query_args: HashMap<String, String>| RenderContext { .map(move |query_args: HashMap<String, String>| RenderContext {
domain_manager: manager.clone(),
handlebars: hbs.clone(), handlebars: hbs.clone(),
query_args, query_args,
}); });
@ -79,7 +94,7 @@ pub fn new(
.and(warp::path("static")) .and(warp::path("static"))
.and(warp::path::full()) .and(warp::path::full())
.map( .map(
|render_ctx: RenderContext<'_>, full: warp::path::FullPath| { |render_ctx: RenderContext<'_, DM>, full: warp::path::FullPath| {
render(render_ctx.handlebars, full.as_str(), ()) render(render_ctx.handlebars, full.as_str(), ())
}, },
); );
@ -87,31 +102,37 @@ pub fn new(
let index = warp::get() let index = warp::get()
.and(with_render_ctx.clone()) .and(with_render_ctx.clone())
.and(warp::path::end()) .and(warp::path::end())
.map(|render_ctx: RenderContext<'_>| { .map(|render_ctx: RenderContext<'_, DM>| {
render_page(render_ctx, String::from("/index.html"), ()) render_page(render_ctx, String::from("/index.html"), ())
}); });
#[derive(Deserialize)] #[derive(Deserialize)]
struct DomainGetNewRequest { struct DomainGetNewRequest {
domain: String, domain: domain::Name,
} }
let domain_get = warp::get() let domain_get = warp::get()
.and(with_render_ctx.clone()) .and(with_render_ctx.clone())
.and(warp::path("domain.html")) .and(warp::path("domain.html"))
.and(warp::query::<DomainGetNewRequest>()) .and(warp::query::<DomainGetNewRequest>())
.map(|render_ctx: RenderContext<'_>, req: DomainGetNewRequest| { .map(
move |render_ctx: RenderContext<'_, DM>, req: DomainGetNewRequest| {
#[derive(Serialize)] #[derive(Serialize)]
struct DomainGetNewResponse { struct DomainGetNewResponse {
domain: String, domain: domain::Name,
} }
render_page( match render_ctx.domain_manager.get_config(&req.domain) {
Ok(_config) => panic!("TODO"),
Err(domain::manager::GetConfigError::NotFound) => render_page(
render_ctx, render_ctx,
String::from("/domain_get_new.html"), String::from("/domain_get_new.html"),
DomainGetNewResponse { domain: req.domain }, DomainGetNewResponse { domain: req.domain },
) ),
}); Err(_) => panic!("TODO"),
}
},
);
Ok(static_dir.or(index).or(domain_get)) Ok(static_dir.or(index).or(domain_get))
} }