index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-01-24 23:10:51.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-01-24 23:10:51.0 +00:00:00
commit
bff49a0adcf7e68159d5b2eaeb7504f02a0ed0ca [patch]
tree
247d5c0324e605034bedf6341db47315057bcc37
parent
960a681575fa687ca88db0359922df66e5f0b7bd
download
bff49a0adcf7e68159d5b2eaeb7504f02a0ed0ca.tar.gz

refactor: helpers and filters functions



Diff

 src/cache.rs     | 13 +++----------
 src/filters.rs   |  6 ++++++
 src/helpers.rs   |  3 +--
 src/lib.rs       |  1 +
 src/rejection.rs |  2 +-
 src/server.rs    | 14 +++++++-------
 6 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/src/cache.rs b/src/cache.rs
index a9f93dd..c32e1c1 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -1,8 +1,8 @@
const CACHE_EXT_ONE_HOUR: [&str; 4] = ["atom", "json", "rss", "xml"];
const CACHE_EXT_ONE_YEAR: [&str; 30] = [
    "bmp", "bz2", "css", "map", "doc", "gif", "gz", "htc", "ico", "jpg", "mp3", "mp4", "ogg",
    "ogv", "pdf", "png", "rar", "tar", "tgz", "wav", "weba", "webm", "webp", "woff", "zip", "jpeg",
    "js", "mjs", "rtf", "woff2",
    "bmp", "bz2", "css", "doc", "gif", "gz", "htc", "ico", "jpeg", "jpg", "js", "map", "mjs",
    "mp3", "mp4", "ogg", "ogv", "pdf", "png", "rar", "rtf", "tar", "tgz", "wav", "weba", "webm",
    "webp", "woff", "woff2", "zip",
];

/// It applies the corresponding Cache-Control headers based on a set of file types.
@@ -36,10 +36,3 @@ pub fn control_headers(res: warp::fs::File) -> warp::reply::WithHeader<warp::fs:
fn duration(n: u64) -> u32 {
    std::cmp::min(n, u32::MAX as u64) as u32
}

/// Warp filter in order to check for an `Accept-Encoding` header value.
pub fn has_accept_encoding(
    val: &'static str,
) -> impl warp::Filter<Extract = (), Error = warp::Rejection> + Copy {
    warp::header::contains("accept-encoding", val)
}
diff --git a/src/filters.rs b/src/filters.rs
new file mode 100644
index 0000000..b1766bf
--- /dev/null
+++ b/src/filters.rs
@@ -0,0 +1,6 @@
/// Warp filter in order to check for an `Accept-Encoding` header value.
pub fn has_accept_encoding(
    val: &'static str,
) -> impl warp::Filter<Extract = (), Error = warp::Rejection> + Copy {
    warp::header::contains("accept-encoding", val)
}
diff --git a/src/helpers.rs b/src/helpers.rs
index 563c85f..63cb0a9 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -27,11 +27,10 @@ where
    }
}

// Read the entire contents of a file into a string if it's valid or empty otherwise.
/// Read the entire contents of a file into a string if valid or returns empty otherwise.
pub fn read_file_content(p: &str) -> String {
    if !p.is_empty() && Path::new(p).exists() {
        return fs::read_to_string(p).unwrap_or_default();
    }

    String::new()
}
diff --git a/src/lib.rs b/src/lib.rs
index 405778b..da5ef03 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,7 @@ extern crate anyhow;
pub mod cache;
pub mod config;
pub mod cors;
pub mod filters;
pub mod helpers;
pub mod logger;
pub mod rejection;
diff --git a/src/rejection.rs b/src/rejection.rs
index ece347c..3292191 100644
--- a/src/rejection.rs
+++ b/src/rejection.rs
@@ -3,7 +3,7 @@ use std::convert::Infallible;
use warp::http::StatusCode;
use warp::{Rejection, Reply};

// It receives a `Rejection` and tries to return a HTML error reply.
/// It receives a `Rejection` and tries to return the corresponding HTML error reply.
pub async fn handle_rejection(
    page_404: String,
    page_50x: String,
diff --git a/src/server.rs b/src/server.rs
index 4b65ba0..e0ce105 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,7 +1,7 @@
use std::net::{IpAddr, SocketAddr};
use warp::Filter;

use crate::{cache, config, cors, helpers, logger, rejection, signals, Result};
use crate::{cache, config, cors, filters, helpers, logger, rejection, signals, Result};

/// Define a multi-thread HTTP/HTTPS web server.
pub struct Server {
@@ -95,7 +95,7 @@ impl Server {
                    );
                    warp::serve(
                        public_head.with(cors_filter.clone()).or(warp::get()
                            .and(cache::has_accept_encoding("br"))
                            .and(filters::has_accept_encoding("br"))
                            .and(with_dir)
                            .with(cors_filter.clone())
                            .or(public_get_default.with(cors_filter))),
@@ -105,7 +105,7 @@ impl Server {
                } else {
                    warp::serve(
                        public_head.or(warp::get()
                            .and(cache::has_accept_encoding("br"))
                            .and(filters::has_accept_encoding("br"))
                            .and(with_dir)
                            .or(public_get_default)),
                    )
@@ -131,7 +131,7 @@ impl Server {
                    );
                    warp::serve(
                        public_head.with(cors_filter.clone()).or(warp::get()
                            .and(cache::has_accept_encoding("deflate"))
                            .and(filters::has_accept_encoding("deflate"))
                            .and(with_dir)
                            .with(cors_filter.clone())
                            .or(public_get_default.with(cors_filter))),
@@ -141,7 +141,7 @@ impl Server {
                } else {
                    warp::serve(
                        public_head.or(warp::get()
                            .and(cache::has_accept_encoding("deflate"))
                            .and(filters::has_accept_encoding("deflate"))
                            .and(with_dir)
                            .or(public_get_default)),
                    )
@@ -167,7 +167,7 @@ impl Server {
                    );
                    warp::serve(
                        public_head.with(cors_filter.clone()).or(warp::get()
                            .and(cache::has_accept_encoding("gzip"))
                            .and(filters::has_accept_encoding("gzip"))
                            .and(with_dir)
                            .with(cors_filter.clone())
                            .or(public_get_default.with(cors_filter))),
@@ -177,7 +177,7 @@ impl Server {
                } else {
                    warp::serve(
                        public_head.or(warp::get()
                            .and(cache::has_accept_encoding("gzip"))
                            .and(filters::has_accept_encoding("gzip"))
                            .and(with_dir)
                            .or(public_get_default)),
                    )