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

Support clean URLs #12

Merged
merged 1 commit into from
Jul 4, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ the following configuration methods:
| `[MemoryServe::html_cache_control]` | `CacheConrol::Short` | Cache control header to serve on HTML files
| `[MemoryServe::cache_control]` | `CacheConrol::Medium` | Cache control header to serve on other files
| `[MemoryServe::add_alias]` | `[]` | Create a route / file alias
| `[MemoryServe::enable_clean_url]` | `false` | Enable clean URLs

See `Cache control` for the cache control options.

Expand Down
36 changes: 34 additions & 2 deletions memory-serve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct ServeOptions {
cache_control: CacheControl,
enable_brotli: bool,
enable_gzip: bool,
enable_clean_url: bool,
}

impl Default for ServeOptions {
Expand All @@ -40,6 +41,7 @@ impl Default for ServeOptions {
cache_control: CacheControl::Medium,
enable_brotli: !cfg!(debug_assertions),
enable_gzip: !cfg!(debug_assertions),
enable_clean_url: false,
}
}
}
Expand Down Expand Up @@ -113,6 +115,15 @@ impl MemoryServe {
self
}

/// Whether to enable clean URLs. When set to `true`, the routing path for
/// HTML files will not include the extension so that a file located at
/// "/about.html" maps to "/about" instead of "/about.html".
pub fn enable_clean_url(mut self, enable_clean_url: bool) -> Self {
self.options.enable_clean_url = enable_clean_url;

self
}

/// The Cache-Control header to set for HTML files.
/// See [Cache control](index.html#cache-control) for options.
pub fn html_cache_control(mut self, html_cache_control: CacheControl) -> Self {
Expand Down Expand Up @@ -195,7 +206,12 @@ impl MemoryServe {
router = router.route("/", get(handler));
}

router = router.route(asset.route, get(handler));
let path = if options.enable_clean_url && asset.route.ends_with(".html") {
&asset.route[..asset.route.len() - 5]
} else {
asset.route
};
router = router.route(path, get(handler));

// add all aliases that point to the asset route
for (from, to) in self.aliases.iter() {
Expand Down Expand Up @@ -260,6 +276,7 @@ mod tests {
assert_eq!(
routes,
[
"/about.html",
"/assets/icon.jpg",
"/assets/index.css",
"/assets/index.js",
Expand All @@ -270,6 +287,7 @@ mod tests {
assert_eq!(
content_types,
[
"text/html",
"image/jpeg",
"text/css",
"application/javascript",
Expand All @@ -278,11 +296,12 @@ mod tests {
]
);
if cfg!(debug_assertions) {
assert_eq!(etags, ["", "", "", "", ""]);
assert_eq!(etags, ["", "", "", "", "", ""]);
} else {
assert_eq!(
etags,
[
"56a0dcb83ec56b6c967966a1c06c7b1392e261069d0844aa4e910ca5c1e8cf58",
"e64f4683bf82d854df40b7246666f6f0816666ad8cd886a8e159535896eb03d6",
"ec4edeea111c854901385011f403e1259e3f1ba016dcceabb6d566316be3677b",
"86a7fdfd19700843e5f7344a63d27e0b729c2554c8572903ceee71f5658d2ecf",
Expand Down Expand Up @@ -402,6 +421,19 @@ mod tests {
assert_eq!(code, 200);
}

#[tokio::test]
async fn clean_url() {
let memory_router = MemoryServe::new(load_assets!("../static"))
.enable_clean_url(true)
.into_router();

let (code, _) = get(memory_router.clone(), "/about.html", "accept", "*").await;
assert_eq!(code, 404);

let (code, _) = get(memory_router.clone(), "/about", "accept", "*").await;
assert_eq!(code, 200);
}

#[tokio::test]
async fn fallback() {
let memory_router = MemoryServe::new(load_assets!("../static")).into_router();
Expand Down
14 changes: 14 additions & 0 deletions static/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RUST IS AWESOME</title>
<script type="module" crossorigin src="/assets/index.js"></script>
<link rel="stylesheet" href="/assets/index.css">
<link rel="icon" type="image/jpeg" href="/assets/icon.jpg">
</head>
<body>
<h1>About Page</h1>
</body>
</html>
Loading