diff options
author | Norberto Lopes <nlopes.ml@gmail.com> | 2023-08-04 16:00:11 +0000 |
---|---|---|
committer | Norberto Lopes <nlopes.ml@gmail.com> | 2023-08-06 08:34:08 +0000 |
commit | 1fd606031d018854d2b8d08d30863105567fcccc (patch) | |
tree | ad263938cd57951c519f3351b7ca9d9a480c7f4f | |
parent | Add CHANGELOG entry for #1191 (diff) | |
download | miniserve-1fd606031d018854d2b8d08d30863105567fcccc.tar.gz miniserve-1fd606031d018854d2b8d08d30863105567fcccc.zip |
Add pretty urls
This adds a new flag namely `--pretty-urls` that when enabled will serve the equivalent
`.html` if it exists.
Very much the same approach that [`netlify`
uses](https://docs.netlify.com/site-deploys/post-processing/).
It can be quite useful when having hrefs like `/about` serve `/about.html`.
-rw-r--r-- | src/args.rs | 8 | ||||
-rw-r--r-- | src/config.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 24 | ||||
-rw-r--r-- | tests/serve_request.rs | 18 |
4 files changed, 57 insertions, 1 deletions
diff --git a/src/args.rs b/src/args.rs index f7b42f1..590ac14 100644 --- a/src/args.rs +++ b/src/args.rs @@ -41,6 +41,14 @@ pub struct CliArgs { #[arg(long, requires = "index", env = "MINISERVE_SPA")] pub spa: bool, + /// Activate Pretty URLs mode + /// + /// This will cause the server to serve the equivalent `.html` file indicated by the path. + /// + /// `/about` will try to find `about.html` and serve it. + #[arg(long, env = "MINISERVE_PRETTY_URLS")] + pub pretty_urls: bool, + /// Port to use #[arg( short = 'p', diff --git a/src/config.rs b/src/config.rs index 8976d35..2df9fdd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -80,6 +80,13 @@ pub struct MiniserveConfig { /// allow the SPA router to handle the request instead. pub spa: bool, + /// Activate Pretty URLs mode + /// + /// This will cause the server to serve the equivalent `.html` file indicated by the path. + /// + /// `/about` will try to find `about.html` and serve it. + pub pretty_urls: bool, + /// Enable QR code display pub show_qrcode: bool, @@ -250,6 +257,7 @@ impl MiniserveConfig { default_color_scheme_dark, index: args.index, spa: args.spa, + pretty_urls: args.pretty_urls, overwrite_files: args.overwrite_files, show_qrcode: args.qrcode, mkdir_enabled: args.mkdir_enabled, diff --git a/src/main.rs b/src/main.rs index 851f9ab..43878db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ use std::time::Duration; use actix_files::NamedFile; use actix_web::{ - http::header::ContentType, middleware, web, App, HttpRequest, HttpResponse, Responder, + dev::{fn_service, ServiceRequest, ServiceResponse}, + http::header::ContentType, + middleware, web, App, HttpRequest, HttpResponse, Responder, }; use actix_web_httpauth::middleware::HttpAuthentication; use anyhow::Result; @@ -316,6 +318,26 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { } } + if conf.pretty_urls { + log::info!("Pretty URLs enabled."); + files = files.default_handler(fn_service(|req: ServiceRequest| async { + let (req, _) = req.into_parts(); + let conf = req + .app_data::<MiniserveConfig>() + .expect("Could not get miniserve config"); + let mut path_base = req.path()[1..].to_string(); + if path_base.ends_with('/') { + path_base.pop(); + } + if !path_base.ends_with("html") { + path_base = format!("{}.html", path_base); + } + let file = NamedFile::open_async(conf.path.join(path_base)).await?; + let res = file.into_response(&req); + Ok(ServiceResponse::new(req, res)) + })); + } + if conf.show_hidden { files = files.use_hidden_files(); } diff --git a/tests/serve_request.rs b/tests/serve_request.rs index e717525..ac4360e 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -268,6 +268,24 @@ fn serve_index_instead_of_404_in_spa_mode( } #[rstest] +#[case(server_no_stderr(&["--pretty-urls", "--index", FILES[1]]), "/")] +#[case(server_no_stderr(&["--pretty-urls", "--index", FILES[1]]), "test.html")] +#[case(server_no_stderr(&["--pretty-urls", "--index", FILES[1]]), "test")] +fn serve_file_instead_of_404_in_pretty_urls_mode( + #[case] server: TestServer, + #[case] url: &str, +) -> Result<(), Error> { + let body = reqwest::blocking::get(format!("{}{}", server.url(), url))?.error_for_status()?; + let parsed = Document::from_read(body)?; + assert!(parsed + .find(|x: &Node| x.text() == "Test Hello Yes") + .next() + .is_some()); + + Ok(()) +} + +#[rstest] #[case(server(&["--route-prefix", "foobar"]))] #[case(server(&["--route-prefix", "/foobar/"]))] fn serves_requests_with_route_prefix(#[case] server: TestServer) -> Result<(), Error> { |