index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2019-12-26 23:21:07.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2019-12-26 23:21:07.0 +00:00:00
commit
2979f7a0c56d5e3207a14949bcc5f2a19d35f3c9 [patch]
tree
3df4c24e3e182c2ad127ec417d0c8601da3f46ae
parent
8a90486df37a598874ddae215c1d693b79666225
download
2979f7a0c56d5e3207a14949bcc5f2a19d35f3c9.tar.gz

feat: make error pages optional (resolves #6)



Diff

 src/error_page.rs  | 28 +++++++++++++++++++++-------
 src/staticfiles.rs |  2 +-
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index 7a9e41a..3af0272 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -9,19 +9,33 @@ use std::path::Path;

/// Custom Error pages middleware for Iron
pub struct ErrorPage {
    /// HTML file content for 50x errors.
    pub page50x: std::string::String,
    /// HTML file content for 404 errors.
    pub page404: std::string::String,
    pub page404: String,
    /// HTML file content for 50x errors.
    pub page50x: String,
}

impl ErrorPage {
    /// Create a new instance of `ErrorPage` middleware with a given html pages.
    pub fn new<P: AsRef<Path>>(page_50x_path: P, page_404_path: P) -> ErrorPage {
        let page50x = fs::read_to_string(page_50x_path).unwrap();
        let page404 = fs::read_to_string(page_404_path).unwrap();
    pub fn new<P: AsRef<Path>>(page_404_path: P, page_50x_path: P) -> ErrorPage {
        let page404: String;
        let page50x: String;

        if Path::new(&page_404_path.as_ref()).exists() {
            page404 = fs::read_to_string(page_404_path).unwrap();
        } else {
            page404 = String::from("<h2>404</h2><p>Content could not found</p>");
        }

        if Path::new(&page_50x_path.as_ref()).exists() {
            page50x = fs::read_to_string(page_50x_path).unwrap();
        } else {
            page50x = String::from(
                "<h2>50x</h2><p>Service is temporarily unavailable due an unexpected error</p>",
            );
        }

        ErrorPage { page50x, page404 }
        ErrorPage { page404, page50x }
    }
}

diff --git a/src/staticfiles.rs b/src/staticfiles.rs
index 079faab..cd152f1 100644
--- a/src/staticfiles.rs
+++ b/src/staticfiles.rs
@@ -45,8 +45,8 @@ impl StaticFiles {
        chain.link_after(GzipMiddleware);
        chain.link_after(Logger);
        chain.link_after(ErrorPage::new(
            self.opts.page_50x_path.as_str(),
            self.opts.page_404_path.as_str(),
            self.opts.page_50x_path.as_str(),
        ));
        chain
    }