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 7271ff4..619d504 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,23 @@ 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); + 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)); } }; @@ -122,11 +133,10 @@ 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(); - 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 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