domani/src/service.rs
2023-05-11 19:34:05 +02:00

69 lines
1.8 KiB
Rust

use serde::{Deserialize, Serialize};
use std::error::Error;
use std::sync;
use warp::Filter;
use crate::domain;
pub mod http_tpl;
/*
* POST /domain/init (domain, config) -> token
* POST /domain/config (domain, config)
* GET /domain/config (domain) -> config
* GET /domains
*/
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,
}
pub fn new(
_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))
}