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(-)
@@ -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",
@@ -38,10 +38,10 @@ Server can be configured either via environment variables or their equivalent co
@@ -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
@@ -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"
@@ -15,7 +15,7 @@ pub struct Options {
#[structopt(long, default_value = "./public", env = "SERVER_ROOT")]
pub root: String,
#[structopt(long, default_value = "./assets", env = "SERVER_ASSETS")]
#[structopt(long, default_value = "./public/assets", env = "SERVER_ASSETS")]
pub assets: String,
#[structopt(
@@ -28,40 +28,37 @@ impl StaticFiles {
pub fn handle(&self) -> Chain {
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,
};
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);
let assets_dirname = match helpers::get_dirname(&assets_dir) {
Err(e) => {
error!("{}", e);
std::process::exit(1)
}
Ok(v) => v,
};
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"