Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pretty urls #1193

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -316,6 +318,31 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
}
}

// Handle --pretty-urls options.
//
// We rewrite the request to append ".html" to the path and serve the file. If the
// path ends with a `/`, we remove it before appending ".html".
//
// This is done to allow for pretty URLs, e.g. "/about" instead of "/about.html".
if conf.pretty_urls {
nlopes marked this conversation as resolved.
Show resolved Hide resolved
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();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/serve_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ fn serve_index_instead_of_404_in_spa_mode(
Ok(())
}

#[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/"]))]
Expand Down