mirror of
https://github.com/static-web-server/static-web-server.git
synced 2026-01-25 05:06:33 +00:00
refactor: improve fallback page path checking and logging (#522)
* refactor: check fallback page path and add log debug if not * refactor: improve fallback page docs and command description
This commit is contained in:
@@ -35,7 +35,7 @@ Options:
|
||||
--page404 <PAGE404>
|
||||
HTML file path for 404 errors. If the path is not specified or simply doesn't exist then the server will use a generic HTML error message. If a relative path is used then it will be resolved under the root directory [env: SERVER_ERROR_PAGE_404=] [default: ./404.html]
|
||||
--page-fallback <PAGE_FALLBACK>
|
||||
HTML file path that is used for GET requests when the requested path doesn't exist. The fallback page is served with a 200 status code, useful when using client routers. If the path is not specified or simply doesn't exist then this feature will not be active [env: SERVER_FALLBACK_PAGE=] [default: ]
|
||||
A HTML file path (not relative to the root) used for GET requests when the requested path doesn't exist. The fallback page is served with a 200 status code, useful when using client routers. If the path doesn't exist then the feature is not activated [env: SERVER_FALLBACK_PAGE=] [default: ]
|
||||
-g, --log-level <LOG_LEVEL>
|
||||
Specify a logging level in lower case. Values: error, warn, info, debug or trace [env: SERVER_LOG_LEVEL=] [default: error]
|
||||
-c, --cors-allow-origins <CORS_ALLOW_ORIGINS>
|
||||
@@ -89,7 +89,7 @@ Options:
|
||||
--log-forwarded-for [<LOG_FORWARDED_FOR>]
|
||||
Log the X-Forwarded-For header for remote IP information [env: SERVER_LOG_FORWARDED_FOR=] [default: false] [possible values: true, false]
|
||||
--trusted-proxies <TRUSTED_PROXIES>
|
||||
A comma separated list of IP addresses to accept the X-Forwarded-For header from. Empty means trust all IPs [env: SERVER_TRUSTED_PROXIES] [default: ""]
|
||||
List of IPs to use X-Forwarded-For from. The default is to trust all [env: SERVER_TRUSTED_PROXIES=]
|
||||
--redirect-trailing-slash [<REDIRECT_TRAILING_SLASH>]
|
||||
Check for a trailing slash in the requested directory URI and redirect permanently (308) to the same path with a trailing slash suffix if it is missing [env: SERVER_REDIRECT_TRAILING_SLASH=] [default: true] [possible values: true, false]
|
||||
--ignore-hidden-files [<IGNORE_HIDDEN_FILES>]
|
||||
|
||||
@@ -48,7 +48,7 @@ HTML file path for 50x errors. If the path is not specified or simply doesn't ex
|
||||
If a relative path is used then it will be resolved under the root directory. Default `./50x.html`
|
||||
|
||||
### SERVER_FALLBACK_PAGE
|
||||
HTML file path that is used for `GET` requests when the requested path doesn't exist. The fallback page is served with a `200` status code, useful when using client routers (e.g. `React Router``). If the path is not specified or simply doesn't exist then this feature will not be active.
|
||||
A HTML file path (not relative to the root) used for `GET` requests when the requested path doesn't exist. The fallback page is served with a `200` status code, useful when using client routers. If the path doesn't exist then the feature is not activated.
|
||||
|
||||
### SERVER_THREADS_MULTIPLIER
|
||||
The number of worker threads multiplier will be multiplied by the number of system CPUs using the formula: `worker threads = number of CPUs * n` where `n` is the value that changes here. When the multiplier value is 0 or 1 then the `number of CPUs` is used. The number of worker threads result should be a number between 1 and 32,768 though it is advised to keep this value on the smaller side. Default one thread per core.
|
||||
|
||||
@@ -19,7 +19,12 @@ static-web-server \
|
||||
|
||||
## Fallback Page for use with Client Routers
|
||||
|
||||
An HTML file path that is used for `GET` requests when the requested path doesn't exist. The fallback page is served with a `200` status code, useful when using client routers like `React Router` or similar. If the path is not specified or simply doesn't exist then this feature will not be active.
|
||||
It is possible to provide a HTML file to be used as fallback page when `GET` request paths dont exist.
|
||||
The fallback page will be served with a `200` status code, useful when using client routers like `React Router` or similar.
|
||||
If the path is not specified or simply doesn't exist then this feature will not be activated.
|
||||
|
||||
!!! info "The fallback page path is not relative to the root"
|
||||
The fallback page is an independent path, so provide a valid relative or absolute path.
|
||||
|
||||
It can be set with the `SERVER_FALLBACK_PAGE` environment variable or with the CLI argument `--page-fallback`.
|
||||
|
||||
|
||||
@@ -14,13 +14,22 @@ use std::path::Path;
|
||||
use crate::{handler::RequestHandlerOpts, helpers, http_ext::MethodExt, Error};
|
||||
|
||||
/// Initializes fallback page processing
|
||||
pub(crate) fn init(path: &Path, handler_opts: &mut RequestHandlerOpts) {
|
||||
handler_opts.page_fallback = helpers::read_bytes_default(path);
|
||||
pub(crate) fn init(file_path: &Path, handler_opts: &mut RequestHandlerOpts) {
|
||||
let found = file_path.is_file();
|
||||
if found {
|
||||
handler_opts.page_fallback =
|
||||
String::from_utf8_lossy(&helpers::read_bytes_default(file_path))
|
||||
.trim()
|
||||
.as_bytes()
|
||||
.to_owned();
|
||||
} else {
|
||||
tracing::debug!("fallback page path not found or not a regular file");
|
||||
}
|
||||
|
||||
server_info!(
|
||||
"fallback page: enabled={}, value=\"{}\"",
|
||||
!handler_opts.page_fallback.is_empty(),
|
||||
path.to_string_lossy()
|
||||
found,
|
||||
file_path.display()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ pub struct General {
|
||||
#[cfg(feature = "fallback-page")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))]
|
||||
#[arg(long, default_value = "", value_parser = value_parser_pathbuf, env = "SERVER_FALLBACK_PAGE")]
|
||||
/// HTML file path that is used for GET requests when the requested path doesn't exist. The fallback page is served with a 200 status code, useful when using client routers. If the path is not specified or simply doesn't exist then this feature will not be active.
|
||||
/// A HTML file path (not relative to the root) used for GET requests when the requested path doesn't exist. The fallback page is served with a 200 status code, useful when using client routers. If the path doesn't exist then the feature is not activated.
|
||||
pub page_fallback: PathBuf,
|
||||
|
||||
#[arg(long, short = 'g', default_value = "error", env = "SERVER_LOG_LEVEL")]
|
||||
|
||||
Reference in New Issue
Block a user