From 6e7315c15d8aacf8254d9f2112f4f472ceb628db Mon Sep 17 00:00:00 2001 From: Mirus Date: Mon, 18 Dec 2023 19:21:15 +0800 Subject: [PATCH 1/3] chore: rename scripts to templates --- src/server.rs | 2 +- src/{scripts => templates}/websocket.html | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{scripts => templates}/websocket.html (100%) diff --git a/src/server.rs b/src/server.rs index 7271ff4..2b14d1b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -122,7 +122,7 @@ async fn static_assets( return Err(tide::Error::from_str(StatusCode::InternalServerError, err)); } }; - let script = format!(include_str!("scripts/websocket.html"), host, port); + let script = format!(include_str!("templates/websocket.html"), host, port); file = format!("{text}{script}").into_bytes(); } let mut response: Response = Body::from_bytes(file).into(); diff --git a/src/scripts/websocket.html b/src/templates/websocket.html similarity index 100% rename from src/scripts/websocket.html rename to src/templates/websocket.html From 1f7f4fbbef0f734908b1fe6403974b335a12b260 Mon Sep 17 00:00:00 2001 From: Mirus Date: Mon, 18 Dec 2023 20:25:24 +0800 Subject: [PATCH 2/3] feat: respond with error page when having errors --- src/server.rs | 20 +++++++++++++++++--- src/templates/error.html | 8 ++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/templates/error.html diff --git a/src/server.rs b/src/server.rs index 2b14d1b..3b3c392 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use std::sync::Arc; +use std::{collections::HashMap, io::ErrorKind}; use async_std::{fs, path::PathBuf, prelude::*, sync::Mutex}; use tide::{listener::Listener, Body, Request, Response, StatusCode}; @@ -103,12 +103,27 @@ async fn static_assets( path.push("index.html"); } let mime = mime_guess::from_path(&path).first_or_text_plain(); + let mut response = Response::new(StatusCode::InternalServerError); + response.set_content_type(mime.to_string().as_str()); // Read the file. let mut file = match fs::read(&path).await { Ok(file) => file, Err(err) => { log::warn!("{}", err); + if mime == "text/html" { + let script = format!(include_str!("templates/websocket.html"), host, port); + let html = format!( + include_str!("templates/error.html"), + script, + err.to_string() + ); + response.set_body(Body::from_string(html)); + if err.kind() == ErrorKind::NotFound { + response.set_status(StatusCode::NotFound); + } + return Ok(response); + } return Err(tide::Error::new(StatusCode::NotFound, err)); } }; @@ -125,8 +140,7 @@ async fn static_assets( let script = format!(include_str!("templates/websocket.html"), host, port); file = format!("{text}{script}").into_bytes(); } - let mut response: Response = Body::from_bytes(file).into(); - response.set_content_type(mime.to_string().as_str()); + response.set_body(Body::from_bytes(file)); Ok(response) } diff --git a/src/templates/error.html b/src/templates/error.html new file mode 100644 index 0000000..16393f1 --- /dev/null +++ b/src/templates/error.html @@ -0,0 +1,8 @@ + + + + Live Server Error + {} + +{} + \ No newline at end of file From 0058ee1de6309b9b39b1a72aa518d50c70d85cbe Mon Sep 17 00:00:00 2001 From: Mirus Date: Mon, 18 Dec 2023 20:32:38 +0800 Subject: [PATCH 3/3] chore: cargo fmt & clippy --- src/main.rs | 2 +- src/server.rs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 92b0ca5..c3a9380 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ struct Args { #[clap(default_value = ".")] root: String, /// Set the listener host [default: LAN IP address] - #[clap(short='s', long)] + #[clap(short = 's', long)] host: Option, /// Set the listener port #[clap(short, long, default_value_t = 8000)] diff --git a/src/server.rs b/src/server.rs index 3b3c392..619d504 100644 --- a/src/server.rs +++ b/src/server.rs @@ -113,11 +113,7 @@ async fn static_assets( log::warn!("{}", err); if mime == "text/html" { let script = format!(include_str!("templates/websocket.html"), host, port); - let html = format!( - include_str!("templates/error.html"), - script, - err.to_string() - ); + let html = format!(include_str!("templates/error.html"), script, err); response.set_body(Body::from_string(html)); if err.kind() == ErrorKind::NotFound { response.set_status(StatusCode::NotFound);