index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-01-21 23:35:35.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-01-21 23:35:35.0 +00:00:00
commit
15f77507cf8423659216af20f8b53073397845fd [patch]
tree
7e9c743c6ee24a55ee28ebca2f224c149faa7489
parent
a3744d49263b37f38edb6bc934ba178433703d05
download
15f77507cf8423659216af20f8b53073397845fd.tar.gz

refactor: minor code improvements



Diff

 src/bin/server.rs   | 20 +++++++++-----------
 src/core/helpers.rs |  2 +-
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/src/bin/server.rs b/src/bin/server.rs
index e8bb341..03a4ed4 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -55,21 +55,19 @@ async fn server(opts: config::Options) -> Result {
    let port = opts.port;

    // Public GET/HEAD endpoints with compression (deflate, gzip, brotli, none)
    let page404_c = page404.clone();
    let page50x_c = page50x.clone();
    match opts.compression.as_ref() {
        "brotli" => tokio::task::spawn(
            warp::serve(
                public_head.or(warp::get()
                    .and(cache::accept_encoding("br"))
                    .and(
                        warp::fs::dir(root_dir.clone())
                        warp::fs::dir(root_dir)
                            .map(cache::control_headers)
                            .with(warp::trace::request())
                            .with(warp::compression::brotli(true))
                            .recover(move |rej| {
                                let page404_c = page404_c.clone();
                                let page50x_c = page50x_c.clone();
                                let page404_c = page404.clone();
                                let page50x_c = page50x.clone();
                                async move {
                                    rejection::handle_rejection(page404_c, page50x_c, rej).await
                                }
@@ -84,13 +82,13 @@ async fn server(opts: config::Options) -> Result {
                public_head.or(warp::get()
                    .and(cache::accept_encoding("deflate"))
                    .and(
                        warp::fs::dir(root_dir.clone())
                        warp::fs::dir(root_dir)
                            .map(cache::control_headers)
                            .with(warp::trace::request())
                            .with(warp::compression::deflate(true))
                            .recover(move |rej| {
                                let page404_c = page404_c.clone();
                                let page50x_c = page50x_c.clone();
                                let page404_c = page404.clone();
                                let page50x_c = page50x.clone();
                                async move {
                                    rejection::handle_rejection(page404_c, page50x_c, rej).await
                                }
@@ -105,13 +103,13 @@ async fn server(opts: config::Options) -> Result {
                public_head.or(warp::get()
                    .and(cache::accept_encoding("gzip"))
                    .and(
                        warp::fs::dir(root_dir.clone())
                        warp::fs::dir(root_dir)
                            .map(cache::control_headers)
                            .with(warp::trace::request())
                            .with(warp::compression::gzip(true))
                            .recover(move |rej| {
                                let page404_c = page404_c.clone();
                                let page50x_c = page50x_c.clone();
                                let page404_c = page404.clone();
                                let page50x_c = page50x.clone();
                                async move {
                                    rejection::handle_rejection(page404_c, page50x_c, rej).await
                                }
diff --git a/src/core/helpers.rs b/src/core/helpers.rs
index 9abc9b1..c9a04cb 100644
--- a/src/core/helpers.rs
+++ b/src/core/helpers.rs
@@ -30,7 +30,7 @@ where
// Read the entire contents of a file into a string if it's valid or 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(String::new());
        return fs::read_to_string(p).unwrap_or_default();
    }

    String::new()