index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2019-12-26 23:32:49.0 +00:00:00
committer GitHub <noreply@github.com> 2019-12-26 23:32:49.0 +00:00:00
commit
93da5ac2be0a850661ae57eb2f76afb63aa3a2b3 [patch]
tree
3df4c24e3e182c2ad127ec417d0c8601da3f46ae
parent
780101e7fd25773a4d45ecb1c6b68d10c4b59021
parent
2979f7a0c56d5e3207a14949bcc5f2a19d35f3c9
download
93da5ac2be0a850661ae57eb2f76afb63aa3a2b3.tar.gz

Merge pull request #7 from joseluisq/develop

Make error pages optional

Diff

 .travis.yml            |  2 ++
 public/404.html        | 17 +++++++++++++++++
 public/50x.html        | 17 +++++++++++++++++
 public/assets/main.css | 14 ++++++++++++--
 public/assets/main.js  |  2 +-
 public/index.html      |  8 +++++---
 src/error_page.rs      | 28 +++++++++++++++++++++-------
 src/staticfiles.rs     |  2 +-
 8 files changed, 76 insertions(+), 14 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index a351ccc..eb5a0dd 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,8 @@
language: rust
sudo: required
dist: trusty
notifications:
  email: false
addons:
  apt:
    packages:
diff --git a/public/404.html b/public/404.html
index 48cca14..192df19 100644
--- a/public/404.html
+++ b/public/404.html
@@ -6,6 +6,23 @@
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>404 Not Found</title>
    <style>
        body {
            font-family: sans-serif;
            color: black;
            text-align: center;
            font-size: 1.25rem;
        }

        h1 a {
            text-decoration: none;
            color: black;
        }

        a {
            color: #0366d6;
        }
    </style>
</head>

<body>
diff --git a/public/50x.html b/public/50x.html
index bfe4647..ea050e6 100644
--- a/public/50x.html
+++ b/public/50x.html
@@ -6,6 +6,23 @@
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>50x Service Unavailable</title>
    <style>
        body {
            font-family: sans-serif;
            color: black;
            text-align: center;
            font-size: 1.25rem;
        }

        h1 a {
            text-decoration: none;
            color: black;
        }

        a {
            color: #0366d6;
        }
    </style>
</head>

<body>
diff --git a/public/assets/main.css b/public/assets/main.css
index 85f066a..cc34e89 100644
--- a/public/assets/main.css
+++ b/public/assets/main.css
@@ -1,5 +1,15 @@
body {
  font-family: Arial, Helvetica, sans-serif;
  color: slategray;
  font-family: sans-serif;
  color: black;
  text-align: center;
  font-size: 1.2rem;
}

h1 a {
  text-decoration: none;
  color: black;
}

a {
  color: #0366d6;
}
diff --git a/public/assets/main.js b/public/assets/main.js
index c2cc898..fecd9f2 100644
--- a/public/assets/main.js
+++ b/public/assets/main.js
@@ -1 +1 @@
(() => console.log("Hello world!"))()
(() => console.log("Static Web Server running!"))()
diff --git a/public/index.html b/public/index.html
index c7c3fc9..41c3206 100644
--- a/public/index.html
+++ b/public/index.html
@@ -5,14 +5,16 @@
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My awesome static web page!</title>
    <title>Static Web Server - A blazing fast static files-serving web server powered by Rust Iron.</title>
    <link rel="stylesheet" href="/assets/main.css">
    <link rel="shortcut icon" href="/assets/favicon.ico">
</head>

<body>
    <p>My awesome static web page!</p>

    <h1>Static Web Server</h1>
    <p>A blazing fast static files-serving web server powered by <a href="https://github.com/iron/iron"
            target="_blank">Rust Iron</a>.</p>
    <p><a href="https://github.com/joseluisq/static-web-server/" target="_blank">View on GitHub</a></p>
    <script src="/assets/main.js"></script>
</body>

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
    }