index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2020-04-21 9:18:37.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2020-04-21 9:18:37.0 +00:00:00
commit
2f7d042125b3f0587097ac24dd443cf83361a56c [patch]
tree
d5a5342fe86b71a484bded4b1bafd076f4542d11
parent
07bf49b0e5e879025f89e9b2e4e70dd72c760ea3
download
2f7d042125b3f0587097ac24dd443cf83361a56c.tar.gz

feat: configurable assets directory

this commit makes assets directory (--assets its equivalent SERVER_ASSETS) configurable.
it means that assets directory is no more restricted to be relative to root directory.
so relative and absolutes *paths* work.

because of the assets directory is treated as independent path.
it's necessary to adjust the assets directory path to be explicit.

for example: having the root `./my-root`
the assets directory path ´./my-assets´ should be changed to `./my-root/my-assets`

Diff

 Cargo.lock         |  2 +-
 README.md          |  8 +++++---
 Tasks.Dev.toml     |  2 +-
 src/config.rs      |  2 +-
 src/staticfiles.rs | 43 ++++++++++++++++++++-----------------------
 5 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 0c34cea..892e55e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -297,7 +297,7 @@ dependencies = [
[[package]]
name = "iron_staticfile_middleware"
version = "0.2.0"
source = "git+https://github.com/joseluisq/iron-staticfile-middleware.git#4e02bc6eda9cf88bf4b7866581ae83efda7f3110"
source = "git+https://github.com/joseluisq/iron-staticfile-middleware.git#68ab4500d43f63493ebb9db5983a94e10193dee0"
dependencies = [
 "iron",
 "log 0.4.8",
diff --git a/README.md b/README.md
index 11ee157..0665025 100644
--- a/README.md
+++ b/README.md
@@ -38,10 +38,10 @@ Server can be configured either via environment variables or their equivalent co
| `SERVER_PORT` | Host port. | Default `80`. |
| `SERVER_ROOT` | Root directory path of static files. | Default `./public`. |
| `SERVER_ASSETS` | Assets directory path for add cache headers functionality. | Default `./assets` but relative to the root. |
| `SERVER_LOG_LEVEL` | Specify a logging level in lower case (see [log::LevelFilter]https://docs.rs/log/0.4.10/log/enum.LevelFilter.html). | Default `error` |
| `SERVER_ERROR_PAGE_404` | HTML file path for 404 errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/404.html`.
| `SERVER_ERROR_PAGE_50X` | HTML file path for 50x errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/50x.html` |
| `SERVER_TLS` | Enables TLS/SSL support. Make sure also to adjust current server port. | Default `false` |
| `SERVER_LOG_LEVEL` | Specify a logging level in lower case (see [log::LevelFilter]https://docs.rs/log/0.4.10/log/enum.LevelFilter.html). | Default `error` |
| `SERVER_TLS_PKCS12` | A cryptographic identity [PKCS #12]https://docs.rs/native-tls/0.2.3/native_tls/struct.Identity.html#method.from_pkcs12 bundle file path containing a [X509 certificate]https://en.wikipedia.org/wiki/X.509 along with its corresponding private key and chain of certificates to a trusted root. | Default empty |
| `SERVER_TLS_PKCS12_PASSWD` | A specified password to decrypt the private key. | Default empty |

@@ -62,10 +62,12 @@ FLAGS:

OPTIONS:
        --assets <assets>
            Assets directory path for add cache headers functionality [env: SERVER_ASSETS=]  [default: ./assets]
            Assets directory path for add cache headers functionality [env: SERVER_ASSETS=]  [default: ./public/assets]

        --host <host>                              Host address (E.g 127.0.0.1) [env: SERVER_HOST=]  [default: [::]]
        --log-level <log-level>                    Specify a logging level in lower case [env: SERVER_LOG_LEVEL=]  [default: error]
        --log-level <log-level>
            Specify a logging level in lower case [env: SERVER_LOG_LEVEL=]  [default: error]

        --name <name>                              Name for server [env: SERVER_NAME=]  [default: my-static-server]
        --page404 <page404>
            HTML file path for 404 errors. If path is not specified or simply don't exists then server will use a
diff --git a/Tasks.Dev.toml b/Tasks.Dev.toml
index 625d7a7..495fe2a 100644
--- a/Tasks.Dev.toml
+++ b/Tasks.Dev.toml
@@ -3,7 +3,7 @@ E_ARGS = "--port=8787"
E_URL = "http://locahost"
SERVER_LOG_LEVEL = "trace"
SERVER_ROOT = "./public"
SERVER_ASSETS = "./assets"
SERVER_ASSETS = "./public/assets"

[tasks.watch]
command = "cargo"
diff --git a/src/config.rs b/src/config.rs
index 848364b..8223481 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -15,7 +15,7 @@ pub struct Options {
    #[structopt(long, default_value = "./public", env = "SERVER_ROOT")]
    /// Root directory path of static files
    pub root: String,
    #[structopt(long, default_value = "./assets", env = "SERVER_ASSETS")]
    #[structopt(long, default_value = "./public/assets", env = "SERVER_ASSETS")]
    /// Assets directory path for add cache headers functionality
    pub assets: String,
    #[structopt(
diff --git a/src/staticfiles.rs b/src/staticfiles.rs
index 056af6e..dcb0e0a 100644
--- a/src/staticfiles.rs
+++ b/src/staticfiles.rs
@@ -28,40 +28,37 @@ impl StaticFiles {
    /// Handle static files for current `StaticFiles` middleware.
    pub fn handle(&self) -> Chain {
        // Check the root directory
        let root_dir = match helpers::validate_dirpath(&self.opts.root_dir) {
            Err(err) => {
                error!(
                    "{}",
                    helpers::path_error_fmt(err, "root", &self.opts.root_dir)
                );
                std::process::exit(1);
        let root_dir = match helpers::get_valid_dirpath(&self.opts.root_dir) {
            Err(e) => {
                error!("{}", e);
                std::process::exit(1)
            }
            Ok(val) => val,
            Ok(v) => v,
        };

        // Check the assets directory
        let assets_dir = match helpers::validate_dirpath(&self.opts.assets_dir) {
            Err(err) => {
                error!(
                    "{}",
                    helpers::path_error_fmt(err, "assets", &self.opts.assets_dir)
                );
                std::process::exit(1);
        let assets_dir = match helpers::get_valid_dirpath(&self.opts.assets_dir) {
            Err(e) => {
                error!("{}", e);
                std::process::exit(1)
            }
            Ok(val) => val,
            Ok(v) => v,
        };

        let assets_dirname = match assets_dir.iter().last() {
            Some(val) => val.to_str().unwrap().to_string(),
            None => {
                error!("assets directory name was not determined");
                std::process::exit(1);
        // Get the assets directory name
        let assets_dirname = match helpers::get_dirname(&assets_dir) {
            Err(e) => {
                error!("{}", e);
                std::process::exit(1)
            }
            Ok(v) => v,
        };

        // Define middleware chain
        let mut chain =
            Chain::new(Staticfile::new(&root_dir).expect("Directory to serve files was not found"));
        let mut chain = Chain::new(
            Staticfile::new(&root_dir, &assets_dir)
                .expect("Directory to serve files was not found"),
        );
        let one_day = Duration::new(60 * 60 * 24, 0);
        let one_year = Duration::new(60 * 60 * 24 * 365, 0);
        let default_content_type = "text/html"