Skip to content

Commit

Permalink
refactor: use request parts for responder, not whole request
Browse files Browse the repository at this point in the history
fix: minor fixes
  • Loading branch information
peku33 committed Jul 6, 2024
1 parent 8285789 commit d0e2386
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 94 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ web-static-pack is a set of tools for embedding static resources (GUI, assets, i

It consists of two parts:
- [web-static-pack-packer](https://crates.io/crates/web-static-pack-packer) (aka "packer") - a standalone application (can be used as a library) used to serialize your assets into single file, called `pack`. It will usually be used before you build your target application (eg. in build script / CI / build.rs). During creation of a `pack` all heavy computations are done, eg. calculating `ETag`, compressed (`gzip`, `brotli`) versions, mime guessing etc. As a result a `pack` file is created, to be used by the next part.
- [web-static-pack](https://crates.io/crates/web-static-pack) (aka "loader") - a library to include in your target application that will read the `pack` (preferably included in the application with <https://docs.rs/include_bytes_aligned/latest/include_bytes_aligned/>). Then `pack` can be used to form a `http` `service` (a function taking a request and returning response) serving files from the `pack`.
- [web-static-pack](https://crates.io/crates/web-static-pack) (aka "loader") - a library to include in your target application that will read the `pack` (preferably included in the application with <https://docs.rs/include_bytes_aligned/latest/include_bytes_aligned/>). Then `pack` can be used to form a `http` `service` (a function taking a request (parts) and returning response) serving files from the `pack`.

## Features
- Precomputed (in "packer") `ETag` (using `sha3`), compressed bodies (in `gzip` and `brotli` formats), `content-type`, etc. This reduces both runtime overhead and dependencies of your target application.
Expand Down Expand Up @@ -66,7 +66,13 @@ async fn main() -> Result<(), Error> {
let service_fn = service_fn(|request: Request<Incoming>| async {
// you can probably filter your /api requests here
let (parts, _body) = request.into_parts();
let response = responder.respond_flatten(parts);

let response = responder.respond_flatten(
&parts.method,
parts.uri.path(),
&parts.headers,
);

Ok::<_, Infallible>(response)
});

Expand All @@ -89,7 +95,7 @@ The 0.5.0 is almost a complete rewrite, however the general idea remains the sam
- Since we no longer depend on `hyper` in any way (the `http` crate is common interface), `hyper_loader` feature is no longer present in loader.
- `let loader = loader::Loader::new(...)` is now `let pack_archived = loader::load(...)`. This value is still used for `Responder::new`.
- `hyper_loader::Responder` is now just `responder::Responder`, and it's now built around `http` crate, compatible with `hyper` 1.0.
- `Responder` was rewritten. It now accepts request parts (without body) not whole request. `request_respond_or_error` and `parts_respond_or_error` are now `respond`. `request_respond` and `parts_respond` are now `respond_flatten`.
- `Responder` was rewritten. It now accepts (method, path, headers) not whole request. `request_respond_or_error` and `parts_respond_or_error` are squashed to `respond`. `request_respond` and `parts_respond` are squashed to `respond_flatten`.

### New features and improvements
- True zero-copy deserialization with `rkyv`.
Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "web-static-pack-common"
version = "0.5.0-beta.1"
version = "0.5.0-beta.2"
authors = ["Paweł Kubrak <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions loader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "web-static-pack"
version = "0.5.0-beta.1"
version = "0.5.0-beta.2"
authors = ["Paweł Kubrak <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -12,7 +12,7 @@ keywords = ["web", "http", "static", "resources", "hyper"]
categories = ["web-programming"]

[dependencies]
web-static-pack-common = { version = "0.5.0-beta.1", path = "../common" }
web-static-pack-common = { version = "0.5.0-beta.2", path = "../common" }

anyhow = "1.0.86"
http = "1.1.0"
Expand Down
20 changes: 15 additions & 5 deletions loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ http service and used with a web server like hyper.

The main part of this crate is [responder::Responder]. Its
[responder::Responder::respond_flatten] method makes a [http] service - a
function taking [http::Request] (actually the [http::request::Parts], as we
don't need the body) and returning [http::Response].
function taking [http::Request] parts (method, path, headers) and returning
[http::Response].

To make a [responder::Responder], a [common::pack::Pack] is needed. It can
be obtained by [loader::load] function by passing (possibly included in
Expand All @@ -28,7 +28,7 @@ binary) contents of a `pack` created with the packer.
```rust
use anyhow::Error;
use include_bytes_aligned::include_bytes_aligned;
use http::StatusCode;
use http::{HeaderMap, Method, StatusCode};
use web_static_pack::{loader::load, responder::Responder};

// assume we have a vcard-personal-portfolio.pack available from packer examples
Expand All @@ -44,7 +44,11 @@ fn main() -> Result<(), Error> {

// do some checks on the responder
assert_eq!(
responder.respond_flatten(<present file request>).status(),
responder.respond_flatten(
&Method::GET,
"/present",
&HeaderMap::default(),
).status(),
StatusCode::OK
);

Expand Down Expand Up @@ -84,7 +88,13 @@ async fn main() -> Result<(), Error> {
let service_fn = service_fn(|request: Request<Incoming>| async {
// you can probably filter your /api requests here
let (parts, _body) = request.into_parts();
let response = responder.respond_flatten(parts);

let response = responder.respond_flatten(
&parts.method,
parts.uri.path(),
&parts.headers
);

Ok::<_, Infallible>(response)
});

Expand Down
6 changes: 3 additions & 3 deletions loader/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::{
};
use http::HeaderValue;

/// Trait for single file inside a `pack`. Consists of body in different encodings
/// (`identity` aka `normal`, `gzip`, `brotli`), some precomputed header values
/// etc.
/// Trait for single file inside a `pack`. Consists of body in different
/// encodings (`identity` aka `normal`, `gzip`, `brotli`), some precomputed
/// header values etc.
///
/// Most users will indirectly use [FileArchived] implementation, obtained from
/// [crate::pack::Pack::get_file_by_path] (implemented by
Expand Down
20 changes: 15 additions & 5 deletions loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//!
//! The main part of this crate is [responder::Responder]. Its
//! [responder::Responder::respond_flatten] method makes a [http] service - a
//! function taking [http::Request] (actually the [http::request::Parts], as we
//! don't need the body) and returning [http::Response].
//! function taking [http::Request] parts (method, path, headers) and returning
//! [http::Response].
//!
//! To make a [responder::Responder], a [common::pack::Pack] is needed. It can
//! be obtained by [loader::load] function by passing (possibly included in
Expand All @@ -26,7 +26,7 @@
//! ```ignore
//! use anyhow::Error;
//! use include_bytes_aligned::include_bytes_aligned;
//! use http::StatusCode;
//! use http::{HeaderMap, Method, StatusCode};
//! use web_static_pack::{loader::load, responder::Responder};
//!
//! // assume we have a vcard-personal-portfolio.pack available from packer examples
Expand All @@ -42,7 +42,11 @@
//!
//! // do some checks on the responder
//! assert_eq!(
//! responder.respond_flatten(<present file request>).status(),
//! responder.respond_flatten(
//! &Method::GET,
//! "/present",
//! &HeaderMap::default(),
//! ).status(),
//! StatusCode::OK
//! );
//!
Expand Down Expand Up @@ -82,7 +86,13 @@
//! let service_fn = service_fn(|request: Request<Incoming>| async {
//! // you can probably filter your /api requests here
//! let (parts, _body) = request.into_parts();
//! let response = responder.respond_flatten(parts);
//!
//! let response = responder.respond_flatten(
//! &parts.method,
//! parts.uri.path(),
//! &parts.headers
//! );
//!
//! Ok::<_, Infallible>(response)
//! });
//!
Expand Down
Loading

0 comments on commit d0e2386

Please sign in to comment.