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

Doc fixes #14

Merged
merged 4 commits into from
Jul 22, 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
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ memory-serve is designed to work with [axum](https://github.com/tokio-rs/axum)
## Usage

Provide a relative path to the directory containing your static assets
to the `load_assets!` macro. This macro creates a data structure intended to
be consumed by `[MemoryServe::new]`. Calling `[MemoryServe::into_router()]` on
to the [`load_assets!`] macro. This macro creates a data structure intended to
be consumed by [`MemoryServe::new`]. Calling [`MemoryServe::into_router()`] on
the resulting instance produces a axum
[Router](https://docs.rs/axum/latest/axum/routing/struct.Router.html) that
[`Router`](https://docs.rs/axum/latest/axum/routing/struct.Router.html) that
can either be merged in another `Router` or used directly in a server by
calling `Router::into_make_service()`.
calling [`Router::into_make_service()`](https://docs.rs/axum/latest/axum/routing/struct.Router.html#method.into_make_service).

## Example

Expand Down Expand Up @@ -69,19 +69,19 @@ async fn main() {
An instance of the `MemoryServe` struct can be configured by calling
the following configuration methods:

| method | Default value | Description |
|--------|---------------|-------------|
| `[MemoryServe::index_file]` | `Some("/index.html")` | Which file to serve on the route "/"
| `[MemoryServe::fallback]` | `None` | Which file to serve if no routed matched the request
| `[MemoryServe::fallback_status]` | `StatusCode::NOT_FOUND` | The HTTP status code to routes that did not match
| `[MemoryServe::enable_gzip]` | `true` | Allow to serve gzip encoded files
| `[MemoryServe::enable_brotli]` | `true` | Allow to serve brotli encoded files
| `[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
| method | Default value | Description |
| ----------------------------------- | ----------------------- | ---------------------------------------------------- |
| [`MemoryServe::index_file`] | `Some("/index.html")` | Which file to serve on the route "/" |
| [`MemoryServe::fallback`] | `None` | Which file to serve if no routed matched the request |
| [`MemoryServe::fallback_status`] | `StatusCode::NOT_FOUND` | The HTTP status code to routes that did not match |
| [`MemoryServe::enable_gzip`] | `true` | Allow to serve gzip encoded files |
| [`MemoryServe::enable_brotli`] | `true` | Allow to serve brotli encoded files |
| [`MemoryServe::html_cache_control`] | `CacheControl::Short` | Cache control header to serve on HTML files |
| [`MemoryServe::cache_control`] | `CacheControl::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.
See [`Cache control`](#cache-control) for the cache control options.

## Logging

Expand All @@ -93,8 +93,8 @@ WARN skipping file "static/empty.txt": file empty
```

When running the resulting executable, all registered routes and asset
sizes are logged using the [tracing](https://github.com/tokio-rs/tracing)
crate. To print or log them, use `tracing-subscriber`.
sizes are logged using the [`tracing`](https://docs.rs/tracing/latest/tracing/)
crate. To print or log them, use [`tracing-subscriber`](https://docs.rs/tracing/latest/tracing_subscriber/).
Example output:

```txt
Expand All @@ -116,10 +116,10 @@ Example output:

There are 5 different values to choose from for the cache-control settings:

| Option | Description | Value |
|--------|-------------|-------|
| `[CacheControl::Long]` | clients can keep assets that have cache busting for a year | `max-age=31536000, immutable`
| `[CacheControl::Medium]` | assets without cache busting are revalidated after a day and can be kept for a week | `max-age=604800, stale-while-revalidate=86400`
| `[CacheControl::Short]` | cache kept for max 5 minutes, only at the client (not in a proxy) | `max-age:300, private`
| `[CacheControl::NoCache]` | do not cache if freshness is really vital | `no-cache`
| `[CacheControl::Custom]` | Custom value | *user defined*
| Option | Description | Value |
| ------------------------- | ----------------------------------------------------------------------------------- | ---------------------------------------------- |
| [`CacheControl::Long`] | clients can keep assets that have cache busting for a year | `max-age=31536000, immutable` |
| [`CacheControl::Medium`] | assets without cache busting are revalidated after a day and can be kept for a week | `max-age=604800, stale-while-revalidate=86400` |
| [`CacheControl::Short`] | cache kept for max 5 minutes, only at the client (not in a proxy) | `max-age:300, private` |
| [`CacheControl::NoCache`] | do not cache if freshness is really vital | `no-cache` |
| [`CacheControl::Custom`] | Custom value | _user defined_ |
25 changes: 9 additions & 16 deletions memory-serve/src/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ use axum::http::{header::CACHE_CONTROL, HeaderName, HeaderValue};
/// See [Cache control](index.html#cache-control)
#[derive(Debug, Clone, Copy)]
pub enum CacheControl {
/// clients can keep assets that have cache busting for a year: `"max-age=31536000, immutable"`
Long,
/// assets without cache busting are revalidated after a day and can be kept for a week: `"max-age=604800, stale-while-revalidate=86400"`
Medium,
/// cache kept for max 5 minutes, only at the client (not in a proxy): `"max-age:300, private"`
Short,
/// do not cache if freshness is really vital: `"no-cache"`
NoCache,
/// custom value
Custom(&'static str),
}

impl CacheControl {
pub(crate) fn as_header(&self) -> (HeaderName, HeaderValue) {
let value = match self {
Self::Long => {
// clients can keep assets that have cache busting for a year
"max-age=31536000, immutable"
}
Self::Medium => {
// assets without cache busting are revalidated after a day and can be kept for a week
"max-age=604800, stale-while-revalidate=86400"
}
Self::Short => {
// cache kept for max 5 minutes, only at the client (not in a proxy)
"max-age:300, private"
}
Self::NoCache => {
// do not cache if freshness is really vital
"no-cache"
}
Self::Long => "max-age=31536000, immutable",
Self::Medium => "max-age=604800, stale-while-revalidate=86400",
Self::Short => "max-age:300, private",
Self::NoCache => "no-cache",
Self::Custom(value) => value,
};

Expand Down
2 changes: 1 addition & 1 deletion memory-serve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use crate::{asset::Asset, cache_control::CacheControl};

/// Macro to load a directory of static files into the resulting binary
/// (possibly compressed) and create a data structure of (meta)data
/// as an input for [MemoryServe::new]
/// as an input for [`MemoryServe::new`]
pub use memory_serve_macros::load_assets;

#[derive(Debug, Clone, Copy)]
Expand Down