Skip to content
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
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"
[workspace.package]
rust-version = "1.90.0"
edition = "2024"
version = "0.0.4"
version = "0.0.5"
license = "GPL-3.0"
repository = "https://github.com/rm-dr/servable"
readme = "README.md"
Expand Down Expand Up @@ -44,7 +44,7 @@ mutex_atomic = "deny"
needless_raw_strings = "deny"
str_to_string = "deny"
string_add = "deny"
string_to_string = "deny"
implicit_clone = "deny"
use_debug = "allow"
verbose_file_reads = "deny"
large_types_passed_by_value = "deny"
Expand Down Expand Up @@ -74,6 +74,7 @@ axum = "0.8"
chrono = "0.4"
image = "0.25"
maud = "0.27"
mime = "0.3"
rand = "0.9"
serde = { version = "1.0", features = ["derive"] }
serde_urlencoded = "0.7"
Expand Down
1 change: 1 addition & 0 deletions crates/servable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde_urlencoded = { workspace = true }
tower = { workspace = true }
tracing = { workspace = true }
rand = { workspace = true }
mime = { workspace = true }

tokio = { workspace = true, optional = true }
image = { workspace = true, optional = true }
Expand Down
23 changes: 11 additions & 12 deletions crates/servable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn main() {
"/hello",
StaticAsset {
bytes: b"Hello, World!",
mime: MimeType::Text,
mime: mime::TEXT_PLAIN
},
);

Expand All @@ -56,11 +56,11 @@ The `Servable` trait is the foundation of this stack. \

- `StaticAsset`, for static files like CSS, JavaScript, images, or plain bytes:
```rust
use servable::{StaticAsset, mime::MimeType};
use servable::{StaticAsset};

let asset = StaticAsset {
bytes: b"body { color: red; }",
mime: MimeType::Css,
mime: mime::TEXT_CSS,
ttl: StaticAsset::DEFAULT_TTL
};
```
Expand Down Expand Up @@ -102,11 +102,11 @@ The `Servable` trait is the foundation of this stack. \
A `ServableRouter` exposes a collection of `Servable`s under different routes. It implements `tower`'s `Service` trait, and can be easily be converted into an Axum `Router`. Construct one as follows:

```rust
# use servable::{ServableRouter, StaticAsset, mime::MimeType};
# let home_page = StaticAsset { bytes: b"home", mime: MimeType::Html, ttl: StaticAsset::DEFAULT_TTL};
# let about_page = StaticAsset { bytes: b"about", mime: MimeType::Html, ttl: StaticAsset::DEFAULT_TTL };
# let stylesheet = StaticAsset { bytes: b"css", mime: MimeType::Css, ttl: StaticAsset::DEFAULT_TTL };
# let custom_404_page = StaticAsset { bytes: b"404", mime: MimeType::Html, ttl: StaticAsset::DEFAULT_TTL };
# use servable::{ServableRouter, StaticAsset};
# let home_page = StaticAsset { bytes: b"home", mime: mime::TEXT_HTML, ttl: StaticAsset::DEFAULT_TTL};
# let about_page = StaticAsset { bytes: b"about", mime: mime::TEXT_HTML, ttl: StaticAsset::DEFAULT_TTL };
# let stylesheet = StaticAsset { bytes: b"css", mime: mime::TEXT_CSS, ttl: StaticAsset::DEFAULT_TTL };
# let custom_404_page = StaticAsset { bytes: b"404", mime: mime::TEXT_HTML, ttl: StaticAsset::DEFAULT_TTL };
let route = ServableRouter::new()
.add_page("/", home_page)
.add_page("/about", about_page)
Expand All @@ -121,13 +121,13 @@ let route = ServableRouter::new()

When `image` is enabled, the image below...
```rust
# use servable::{ServableRouter, StaticAsset, mime::MimeType};
# use servable::{ServableRouter, StaticAsset};
let route = ServableRouter::new()
.add_page(
"/image.png",
StaticAsset {
bytes: b"fake image data",
mime: MimeType::Png,
mime: mime::IMAGE_PNG,
ttl: StaticAsset::DEFAULT_TTL
}
);
Expand Down Expand Up @@ -184,13 +184,12 @@ whenever the server is restarted:
```rust
use chrono::TimeDelta;
use servable::{HtmlPage, CACHE_BUST_STR, ServableWithRoute, StaticAsset, ServableRouter};
use servable::mime::MimeType;

pub static HTMX: ServableWithRoute<StaticAsset> = ServableWithRoute::new(
|| format!("/{}/main.css", *CACHE_BUST_STR),
StaticAsset {
bytes: "div{}".as_bytes(),
mime: MimeType::Css,
mime: mime::TEXT_CSS,
ttl: StaticAsset::DEFAULT_TTL,
},
);
Expand Down
6 changes: 2 additions & 4 deletions crates/servable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// and needs a different relative path than cargo build.
// https://github.com/rust-lang/cargo/issues/13309

pub mod mime;

mod types;

use rand::{Rng, distr::Alphanumeric};
Expand Down Expand Up @@ -47,7 +45,7 @@ pub static CACHE_BUST_STR: std::sync::LazyLock<String> = std::sync::LazyLock::ne
#[cfg(feature = "htmx-2.0.8")]
pub const HTMX_2_0_8: servable::StaticAsset = servable::StaticAsset {
bytes: include_str!("../htmx/htmx-2.0.8.min.js").as_bytes(),
mime: mime::MimeType::Javascript,
mime: mime::TEXT_JAVASCRIPT,
ttl: StaticAsset::DEFAULT_TTL,
};

Expand All @@ -57,6 +55,6 @@ pub const HTMX_2_0_8: servable::StaticAsset = servable::StaticAsset {
#[cfg(feature = "htmx-2.0.8")]
pub const EXT_JSON_1_19_12: servable::StaticAsset = servable::StaticAsset {
bytes: include_str!("../htmx/json-enc-1.9.12.js").as_bytes(),
mime: mime::MimeType::Javascript,
mime: mime::TEXT_JAVASCRIPT,
ttl: StaticAsset::DEFAULT_TTL,
};
Loading