index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2020-04-18 0:00:54.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2020-04-18 0:00:54.0 +00:00:00
commit
2e2a6ac6f7941c450ba56d754a9e214a0c07f508 [patch]
tree
e2b70106a8f199f33c11cb7a36b77d7b3fdb0724
parent
597fa972df6f50f45083742ebf6bcbe6bd85891d
download
2e2a6ac6f7941c450ba56d754a9e214a0c07f508.tar.gz

refactor: simplify error page impl



Diff

 src/error_page.rs | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index 3af0272..34c1aa7 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -1,5 +1,3 @@
extern crate iron;

use iron::mime;
use iron::prelude::*;
use iron::status;
@@ -7,6 +5,10 @@ use iron::AfterMiddleware;
use std::fs;
use std::path::Path;

static PAGE_404: &str = "<h2>404</h2><p>Content could not found</p>";
static PAGE_50X: &str =
    "<h2>50x</h2><p>Service is temporarily unavailable due an unexpected error</p>";

/// Custom Error pages middleware for Iron
pub struct ErrorPage {
    /// HTML file content for 404 errors.
@@ -18,22 +20,17 @@ pub struct ErrorPage {
impl ErrorPage {
    /// Create a new instance of `ErrorPage` middleware with a given html pages.
    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();
        let page404 = if Path::new(&page_404_path.as_ref()).exists() {
            fs::read_to_string(page_404_path).unwrap()
        } else {
            page404 = String::from("<h2>404</h2><p>Content could not found</p>");
        }
            String::from(PAGE_404)
        };

        if Path::new(&page_50x_path.as_ref()).exists() {
            page50x = fs::read_to_string(page_50x_path).unwrap();
        let page50x = if Path::new(&page_50x_path.as_ref()).exists() {
            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>",
            );
        }
            String::from(PAGE_50X)
        };

        ErrorPage { page404, page50x }
    }