♻️ Adding space for a Markdown preview

main
_ 2020-11-10 01:02:59 +00:00
parent 63abdc3a16
commit 13b816fd6e
2 changed files with 15 additions and 9 deletions

View File

@ -98,6 +98,8 @@ pub enum StatusCode {
NotFound, // 404 NotFound, // 404
MethodNotAllowed, // 405 MethodNotAllowed, // 405
RangeNotSatisfiable, // 416 RangeNotSatisfiable, // 416
InternalServerError, // 500
} }
impl Default for StatusCode { impl Default for StatusCode {
@ -120,6 +122,8 @@ impl From <StatusCode> for hyper::StatusCode {
StatusCode::NotFound => Self::NOT_FOUND, StatusCode::NotFound => Self::NOT_FOUND,
StatusCode::MethodNotAllowed => Self::METHOD_NOT_ALLOWED, StatusCode::MethodNotAllowed => Self::METHOD_NOT_ALLOWED,
StatusCode::RangeNotSatisfiable => Self::RANGE_NOT_SATISFIABLE, StatusCode::RangeNotSatisfiable => Self::RANGE_NOT_SATISFIABLE,
StatusCode::InternalServerError => Self::INTERNAL_SERVER_ERROR,
} }
} }
} }

View File

@ -228,12 +228,15 @@ async fn serve_root (
) -> Response ) -> Response
{ {
let s = handlebars.render ("file_server_root", &server_info).unwrap (); let s = handlebars.render ("file_server_root", &server_info).unwrap ();
let body = s.into_bytes ();
serve_html (s)
}
fn serve_html (s: String) -> Response {
let mut resp = Response::default (); let mut resp = Response::default ();
resp resp
.header ("content-type".to_string (), "text/html".to_string ().into_bytes ()) .header ("content-type".to_string (), "text/html".to_string ().into_bytes ())
.body_bytes (body) .body_bytes (s.into_bytes ())
; ;
resp resp
} }
@ -259,14 +262,8 @@ async fn serve_dir (
entries, entries,
server_info, server_info,
}).unwrap (); }).unwrap ();
let body = s.into_bytes ();
let mut resp = Response::default (); serve_html (s)
resp
.header ("content-type".to_string (), "text/html".to_string ().into_bytes ())
.body_bytes (body)
;
resp
} }
#[instrument (level = "debug", skip (f))] #[instrument (level = "debug", skip (f))]
@ -414,6 +411,9 @@ enum InternalResponse {
Root, Root,
ServeDir (ServeDirParams), ServeDir (ServeDirParams),
ServeFile (ServeFileParams), ServeFile (ServeFileParams),
MarkdownError,
ServeMarkdownPreview (String),
} }
async fn internal_serve_all ( async fn internal_serve_all (
@ -550,6 +550,8 @@ pub async fn serve_all (
range, range,
range_requested, range_requested,
}) => serve_file (file.into_inner (), send_body, range, range_requested).await, }) => serve_file (file.into_inner (), send_body, range, range_requested).await,
MarkdownError => serve_error (StatusCode::InternalServerError, "Error while rendering Markdown preview"),
ServeMarkdownPreview (s) => serve_html (s),
} }
} }