2023-05-11 17:34:05 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::sync;
|
2023-05-11 12:19:36 +00:00
|
|
|
use warp::Filter;
|
|
|
|
|
2023-05-11 17:34:05 +00:00
|
|
|
use crate::domain;
|
|
|
|
|
|
|
|
pub mod http_tpl;
|
|
|
|
|
2023-05-11 12:19:36 +00:00
|
|
|
/*
|
|
|
|
* POST /domain/init (domain, config) -> token
|
|
|
|
* POST /domain/config (domain, config)
|
|
|
|
* GET /domain/config (domain) -> config
|
|
|
|
* GET /domains
|
|
|
|
*/
|
|
|
|
|
2023-05-11 17:34:05 +00:00
|
|
|
type Handlebars<'a> = sync::Arc<handlebars::Handlebars<'a>>;
|
|
|
|
|
|
|
|
fn render<'a, T: 'a>(hbs: Handlebars<'a>, name: &'a str, value: &'a T) -> impl warp::Reply
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
// TODO deal with 404
|
|
|
|
let render = hbs
|
|
|
|
.render(name, value)
|
|
|
|
.unwrap_or_else(|err| err.to_string());
|
|
|
|
|
|
|
|
let content_type = mime_guess::from_path(name)
|
|
|
|
.first_or_octet_stream()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
warp::reply::with_header(warp::reply::html(render), "Content-Type", content_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DomainInitRequest {
|
|
|
|
//config: domain::config::Config,
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:19:36 +00:00
|
|
|
pub fn new(
|
2023-05-11 17:34:05 +00:00
|
|
|
_manager: impl domain::manager::Manager,
|
|
|
|
) -> Result<
|
|
|
|
impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone,
|
|
|
|
Box<dyn Error>,
|
|
|
|
> {
|
|
|
|
let hbs = sync::Arc::new(self::http_tpl::get()?);
|
|
|
|
let with_hbs = warp::any().map(move || hbs.clone());
|
|
|
|
|
|
|
|
let index = warp::get()
|
|
|
|
.and(warp::path::end())
|
|
|
|
.and(with_hbs.clone())
|
|
|
|
.map(|hbs: Handlebars<'_>| render(hbs, "/index.html", &()));
|
|
|
|
|
|
|
|
let static_dir = warp::get()
|
|
|
|
.and(warp::path("static"))
|
|
|
|
.and(warp::path::full())
|
|
|
|
.and(with_hbs.clone())
|
|
|
|
.map(|full: warp::path::FullPath, hbs: Handlebars<'_>| render(hbs, full.as_str(), &()));
|
|
|
|
|
|
|
|
//filter = warp::path!("domain" / "init").and(warp::post())
|
|
|
|
// .and(warp::query::<DomainInitRequest>())
|
|
|
|
// .map(|q: DomainInitRequest| {
|
|
|
|
// let config_hash = q.config.hash().expect("TODO");
|
|
|
|
// warp::reply::html(config_hash)
|
|
|
|
// });
|
|
|
|
|
|
|
|
Ok(index.or(static_dir))
|
2023-05-11 12:19:36 +00:00
|
|
|
}
|