diff --git a/Cargo.toml b/Cargo.toml index 249355d..acb010b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,10 +16,11 @@ anyhow = "1.0.86" async-trait = "0.1.82" clap = { version = "4.5.17", features = ["derive"] } libpt = { version = "0.7.1", features = ["cli", "log"] } +minijinja = { version = "2.2.0", optional = true } rand = "0.8.5" serde = { version = "1.0.209", features = ["derive"] } serde_json = "1.0.128" -tide = { version = "0.16.0", optional = true } +thiserror = "1.0.63" tokio = { version = "1.40.0", features = [ "macros", "net", @@ -28,7 +29,8 @@ tokio = { version = "1.40.0", features = [ "rt", "sync", ] } +warp = { version = "0.3.7", optional = true } [features] default = ["admin-interface"] -admin-interface = ["dep:tide"] +admin-interface = ["dep:warp", "dep:minijinja"] diff --git a/data/www/admin.html b/data/www/admin.html index c167208..6b5abd9 100644 --- a/data/www/admin.html +++ b/data/www/admin.html @@ -7,7 +7,7 @@ - + Starter Template ยท Bootstrap v5.3 @@ -21,19 +21,15 @@ integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> - - - - - - - + + + + + + + + Bootstrap + + + + - - - - - - - Bootstrap - - - - - +
- + - Starter template + {TITLE}
diff --git a/src/challenge/admin.rs b/src/challenge/admin.rs index 7ccee5f..fdc0e60 100644 --- a/src/challenge/admin.rs +++ b/src/challenge/admin.rs @@ -1,41 +1,75 @@ +use std::fmt::Display; +use std::sync::Arc; + use anyhow::Result; +use libpt::log::warn; +use minijinja::context; +use minijinja::Environment; use serde; -use tide::http::mime; -use tide::prelude::*; -use tide::Request; -use tide::Response; +use thiserror::Error; +use warp::reject; +use warp::reply::Reply; +use warp::reply::Response; +use warp::Filter; use crate::config::Config; use crate::vault::VaultRef; #[derive(Clone)] -pub struct Interface { +pub struct Service<'tp> { vault: VaultRef, config: Config, + env: Environment<'tp>, +} +impl<'tp> Service<'tp> { + fn new(vault: VaultRef, config: Config, env: Environment<'tp>) -> Arc { + Self { vault, config, env }.into() + } } pub async fn serve(vault: VaultRef, config: Config) -> Result<()> { - let mut app = tide::with_state(Interface { vault, config }); - app.at("/").get(overview); - app.at("/styles.css").get(styles); - app.listen("127.0.0.1:8000").await?; + let mut env = Environment::new(); + env.add_template("index", include_str!("../../data/www/admin.html"))?; + + let service = Service::new(vault, config, env); + let service2 = service.clone(); + + let routes = warp::path::end() + .map(move || service2.clone()) + .and_then(overview); + + warp::serve(routes) + .run(service.config.addr_admin.unwrap()) + .await; + + warn!("exited the admin interface"); Ok(()) } -async fn overview(req: Request) -> tide::Result { - let r = Response::builder(200) - .content_type(mime::HTML) - .body(format!( - include_str!("../../data/www/admin.html"), - TITLE = "Wooly-Vault" - )); +#[derive(Debug, Error)] +struct TemplateError(#[from] minijinja::Error); - Ok(r.into()) +impl reject::Reject for TemplateError {} +impl Display for TemplateError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "something went wrong with producing this page") + } } -async fn styles(_req: Request) -> tide::Result { - let r = Response::builder(200) - .content_type(mime::CSS) - .body(include_str!("../../data/www/styles.css")); - Ok(r.into()) +async fn overview(serv: Arc>) -> Result, warp::Rejection> { + let r = Response::new( + serv.env + .get_template("index") + .map_err(TemplateError::from)? + .render(context!(TITLE => "Wooly-Vault", AUTHOR => env!("CARGO_PKG_AUTHORS"))) + .map_err(TemplateError::from)? + .into(), + ); + + Ok(Box::new(r)) +} + +async fn styles() -> Result, warp::Rejection> { + let r = include_str!("../../data/www/styles.css").to_string(); + Ok(Box::new(r)) }