diff --git a/ptth_handlebars/relay_root.html b/handlebars/relay/relay_root.html similarity index 100% rename from ptth_handlebars/relay_root.html rename to handlebars/relay/relay_root.html diff --git a/ptth_handlebars/relay_server_list.html b/handlebars/relay/relay_server_list.html similarity index 100% rename from ptth_handlebars/relay_server_list.html rename to handlebars/relay/relay_server_list.html diff --git a/ptth_handlebars/test_relay_server_list.html b/handlebars/relay/test_relay_server_list.html similarity index 100% rename from ptth_handlebars/test_relay_server_list.html rename to handlebars/relay/test_relay_server_list.html diff --git a/ptth_handlebars/file_server_dir.html b/handlebars/server/file_server_dir.html similarity index 100% rename from ptth_handlebars/file_server_dir.html rename to handlebars/server/file_server_dir.html diff --git a/ptth_handlebars/file_server_root.html b/handlebars/server/file_server_root.html similarity index 100% rename from ptth_handlebars/file_server_root.html rename to handlebars/server/file_server_root.html diff --git a/ptth_handlebars/test_file_server_dir.html b/handlebars/server/test_file_server_dir.html similarity index 100% rename from ptth_handlebars/test_file_server_dir.html rename to handlebars/server/test_file_server_dir.html diff --git a/src/bin/ptth_file_server.rs b/src/bin/ptth_file_server.rs index b287f05..32a43b5 100644 --- a/src/bin/ptth_file_server.rs +++ b/src/bin/ptth_file_server.rs @@ -108,7 +108,7 @@ async fn main () -> Result <(), Box > { let addr = SocketAddr::from(([0, 0, 0, 0], 4000)); - let handlebars = file_server::load_templates ()?; + let handlebars = file_server::load_templates (&PathBuf::new ())?; let state = Arc::new (ServerState { config: Config { diff --git a/src/bin/ptth_server.rs b/src/bin/ptth_server.rs index e3c0d70..65a8189 100644 --- a/src/bin/ptth_server.rs +++ b/src/bin/ptth_server.rs @@ -14,13 +14,16 @@ struct Opt { #[structopt (long)] config_path: Option , + + #[structopt (long)] + asset_root: Option , } fn main () -> Result <(), Box > { let opt = Opt::from_args (); tracing_subscriber::fmt::init (); - let path = opt.config_path.unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml")); + let path = opt.config_path.clone ().unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml")); let config_file: ptth::server::ConfigFile = ptth::load_toml::load (&path); let mut rt = if false { @@ -37,11 +40,12 @@ fn main () -> Result <(), Box > { runtime::Runtime::new ()? }; - rt.block_on (async { + rt.block_on (async move { ptth::server::run_server ( config_file, ptth::graceful_shutdown::init (), - Some (path) + Some (path), + opt.asset_root ).await })?; diff --git a/src/lib.rs b/src/lib.rs index 790562a..d0551bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,7 +133,7 @@ mod tests { let (stop_server_tx, stop_server_rx) = oneshot::channel (); let task_server = { spawn (async move { - server::run_server (config_file, stop_server_rx, None).await.unwrap (); + server::run_server (config_file, stop_server_rx, None, None).await.unwrap (); }) }; diff --git a/src/relay/mod.rs b/src/relay/mod.rs index 4c3dad7..ce04aaf 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -147,7 +147,7 @@ impl From <&ConfigFile> for RelayState { Self { config: Config::from (config_file), - handlebars: Arc::new (load_templates ().unwrap ()), + handlebars: Arc::new (load_templates (&PathBuf::new ()).unwrap ()), request_rendezvous: Default::default (), response_rendezvous: Default::default (), shutdown_watch_tx, @@ -539,17 +539,21 @@ async fn handle_all (req: Request , state: Arc ) }) } -pub fn load_templates () +use std::path::{Path, PathBuf}; + +pub fn load_templates (asset_root: &Path) -> Result , Box > { let mut handlebars = Handlebars::new (); handlebars.set_strict_mode (true); + let asset_root = asset_root.join ("handlebars/relay"); + for (k, v) in vec! [ ("relay_server_list", "relay_server_list.html"), ("relay_root", "relay_root.html"), ].into_iter () { - handlebars.register_template_file (k, format! ("ptth_handlebars/{}", v))?; + handlebars.register_template_file (k, &asset_root.join (v))?; } Ok (handlebars) diff --git a/src/server/file_server.rs b/src/server/file_server.rs index 7d9084b..d89fc43 100644 --- a/src/server/file_server.rs +++ b/src/server/file_server.rs @@ -381,7 +381,7 @@ fn serve_307 (location: String) -> Response { resp } -fn render_markdown (bytes: &[u8]) -> Result { +fn render_markdown (bytes: &[u8], out: &mut String) -> Result <(), MarkdownError> { use pulldown_cmark::{Parser, Options, html}; let markdown_input = match std::str::from_utf8 (bytes) { @@ -393,11 +393,17 @@ fn render_markdown (bytes: &[u8]) -> Result { options.insert (Options::ENABLE_STRIKETHROUGH); let parser = Parser::new_ext (markdown_input, options); + html::push_html (out, parser); + + Ok (()) +} + +fn render_markdown_styled (bytes: &[u8]) -> Result { // Write to String buffer. let mut out = String::new (); out.push_str (""); - html::push_html (&mut out, parser); + render_markdown (bytes, &mut out)?; out.push_str (""); Ok (out) @@ -543,7 +549,7 @@ async fn internal_serve_all ( let bytes_read = file.read (&mut buffer).await.unwrap (); buffer.truncate (bytes_read); - MarkdownPreview (render_markdown (&buffer).unwrap ()) + MarkdownPreview (render_markdown_styled (&buffer).unwrap ()) } } else { @@ -628,17 +634,21 @@ pub async fn serve_all ( } } -pub fn load_templates () +pub fn load_templates ( + asset_root: &Path +) -> Result , Box > { let mut handlebars = Handlebars::new (); handlebars.set_strict_mode (true); + let asset_root = asset_root.join ("handlebars/server"); + for (k, v) in vec! [ ("file_server_dir", "file_server_dir.html"), ("file_server_root", "file_server_root.html"), ].into_iter () { - handlebars.register_template_file (k, format! ("ptth_handlebars/{}", v))?; + handlebars.register_template_file (k, asset_root.join (v))?; } Ok (handlebars) @@ -770,7 +780,7 @@ mod tests { let mut rt = Runtime::new ().unwrap (); rt.block_on (async { - let handlebars = load_templates ().unwrap (); + let handlebars = load_templates (&PathBuf::new ()).unwrap (); let server_info = ServerInfo { server_name: "PTTH File Server".to_string (), }; @@ -879,7 +889,9 @@ mod tests { "

Hello world, this is a complicated very simple example.

\n" ), ].into_iter () { - assert_eq! (expected, &render_markdown (input.as_bytes ()).unwrap ()); + let mut out = String::default (); + render_markdown (input.as_bytes (), &mut out).unwrap (); + assert_eq! (expected, &out); } } } diff --git a/src/server/mod.rs b/src/server/mod.rs index b6bc761..7171c68 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -128,10 +128,13 @@ pub struct Config { pub async fn run_server ( config_file: ConfigFile, shutdown_oneshot: oneshot::Receiver <()>, - hidden_path: Option + hidden_path: Option , + asset_root: Option ) -> Result <(), Box > { + let asset_root = asset_root.unwrap_or_else (|| PathBuf::new ()); + use std::convert::TryInto; if crate::password_is_bad (config_file.api_key.clone ()) { @@ -154,7 +157,7 @@ pub async fn run_server ( .default_headers (headers) .timeout (Duration::from_secs (40)) .build ().unwrap (); - let handlebars = file_server::load_templates ()?; + let handlebars = file_server::load_templates (&asset_root).expect ("Can't load Handlebars templates"); let state = Arc::new (ServerState { config: Config {