From 11515105dd4bd7f81edec2faf1d3b813cbe12ae1 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Mon, 23 Dec 2019 00:48:47 +0100 Subject: [PATCH] feat: error page support (closes #3) --- Makefile | 2 +- public/404.html | 16 ++++++++++++++++ public/50x.html | 16 ++++++++++++++++ src/error_page.rs | 32 ++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/staticfiles.rs | 16 +++++++++------- 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 public/404.html create mode 100644 public/50x.html create mode 100644 src/error_page.rs diff --git a/Makefile b/Makefile index 4c1e3a2..c09fff7 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ test: .PHONY: test fmt: - @cargo fix --edition + @cargo fix @cargo fmt --all .PHONY: fmt diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..48cca14 --- /dev/null +++ b/public/404.html @@ -0,0 +1,16 @@ + + + + + + + + 404 Not Found + + + +

404

+

Content was not found.

+ + + diff --git a/public/50x.html b/public/50x.html new file mode 100644 index 0000000..bfe4647 --- /dev/null +++ b/public/50x.html @@ -0,0 +1,16 @@ + + + + + + + + 50x Service Unavailable + + + +

50x

+

Service is temporarily unavailable.

+ + + diff --git a/src/error_page.rs b/src/error_page.rs new file mode 100644 index 0000000..234f8b0 --- /dev/null +++ b/src/error_page.rs @@ -0,0 +1,32 @@ +extern crate iron; + +use iron::mime; +use iron::prelude::*; +use iron::status; +use iron::AfterMiddleware; + +pub struct ErrorPage; + +impl AfterMiddleware for ErrorPage { + fn after(&self, _: &mut Request, res: Response) -> IronResult { + let content_type = "text/html".parse::().unwrap(); + + match res.status { + Some(status::NotFound) => Ok(Response::with(( + content_type, + status::NotFound, + "404 Not Found", + ))), + Some(status::InternalServerError) => Ok(Response::with(( + content_type, + status::InternalServerError, + "50x Internal Server Error", + ))), + _ => Ok(Response::with(( + content_type, + status::ServiceUnavailable, + "503 Service Unavailable", + ))), + } + } +} diff --git a/src/main.rs b/src/main.rs index 84701c2..6bc28d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use std::io::Write; use structopt::StructOpt; mod config; +mod error_page; mod gzip; mod logger; mod staticfiles; diff --git a/src/staticfiles.rs b/src/staticfiles.rs index 9c058f0..318974f 100644 --- a/src/staticfiles.rs +++ b/src/staticfiles.rs @@ -1,3 +1,4 @@ +use crate::error_page::ErrorPage; use crate::gzip::GzipMiddleware; use crate::logger::Logger; @@ -6,7 +7,7 @@ use iron_staticfile_middleware::{Cache, GuessContentType, ModifyWith, Prefix, St use std::time::Duration; pub fn handler(root_dir: String, assets_dir: String) -> Chain { - let mut files = + let mut chain = Chain::new(Staticfile::new(root_dir).expect("Directory to serve files was not found")); let one_day = Duration::new(60 * 60 * 24, 0); @@ -15,11 +16,12 @@ pub fn handler(root_dir: String, assets_dir: String) -> Chain { .parse() .expect("Unable to create a default content type header"); - files.link_after(ModifyWith::new(Cache::new(one_day))); - files.link_after(Prefix::new(&[assets_dir], Cache::new(one_year))); - files.link_after(GuessContentType::new(default_content_type)); - files.link_after(GzipMiddleware); - files.link_after(Logger); + chain.link_after(ModifyWith::new(Cache::new(one_day))); + chain.link_after(Prefix::new(&[assets_dir], Cache::new(one_year))); + chain.link_after(GuessContentType::new(default_content_type)); + chain.link_after(GzipMiddleware); + chain.link_after(Logger); + chain.link_after(ErrorPage); - files + chain } -- libgit2 1.7.2