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]
pub trait Checker {
pub trait Checker: std::marker::Send + std::marker::Sync {
fn check_domain(
&self,
domain: &domain::Name,

View File

@ -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<Config, GetError>;
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;)]
pub trait Manager {
pub trait Manager: std::marker::Send + std::marker::Sync {
type Origin<'mgr>: origin::Origin + 'mgr
where
Self: 'mgr;

View File

@ -46,7 +46,7 @@ pub type AllDescrsResult<T> = Result<T, AllDescrsError>;
type AllDescrsIter=Vec<AllDescrsResult<origin::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
where
Self: 'store;

View File

@ -16,7 +16,11 @@ pub mod http_tpl;
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>,
query_args: HashMap<String, String>,
}
@ -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<T> {
@ -60,16 +69,22 @@ where
render(render_ctx.handlebars, "/base.html", presenter)
}
pub fn new(
_manager: impl domain::manager::Manager,
pub fn new<DM>(
manager: DM,
) -> 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>,
> {
>
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::<HashMap<String, String>>())
.map(move |query_args: HashMap<String, String>| 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::<DomainGetNewRequest>())
.map(|render_ctx: RenderContext<'_>, req: DomainGetNewRequest| {
.map(
move |render_ctx: RenderContext<'_, DM>, req: DomainGetNewRequest| {
#[derive(Serialize)]
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,
String::from("/domain_get_new.html"),
DomainGetNewResponse { domain: req.domain },
)
});
),
Err(_) => panic!("TODO"),
}
},
);
Ok(static_dir.or(index).or(domain_get))
}