Skip to content

Commit d64d063

Browse files
authored
Middleware-based compression and decompression (#194)
Middleware-based compression and decompression
2 parents dac4493 + 29cdf99 commit d64d063

File tree

7 files changed

+500
-3
lines changed

7 files changed

+500
-3
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ before_script: |
66
rustup component add rustfmt clippy
77
script: |
88
cargo fmt --all -- --check &&
9-
cargo clippy --all -- -D clippy::all &&
9+
cargo clippy --all --all-features -- -D clippy::all &&
1010
cargo build --no-default-features --verbose &&
11-
cargo build --all --verbose &&
12-
cargo test --all --verbose
11+
cargo build --all --all-features --verbose &&
12+
cargo test --all --all-features --verbose
1313
cache: cargo

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"tide",
4+
"tide-compression",
45
"examples",
56
]
67

tide-compression/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
authors = [
3+
"Tide Developers",
4+
]
5+
description = "Compression-related middleware for Tide"
6+
documentation = "https://docs.rs/tide-compression"
7+
keywords = ["tide", "web", "async", "middleware", "compression"]
8+
categories = ["network-programming", "compression", "asynchronous"]
9+
edition = "2018"
10+
license = "MIT OR Apache-2.0"
11+
name = "tide-compression"
12+
readme = "README.md"
13+
repository = "https://github.com/rustasync/tide"
14+
version = "0.1.0"
15+
16+
[dependencies]
17+
tide = { path = "../tide" }
18+
accept-encoding = "0.2.0-alpha.2"
19+
bytes = "0.4.12"
20+
futures-preview = "0.3.0-alpha.16"
21+
http = "0.1"
22+
http-service = "0.2.0"
23+
24+
[dependencies.async-compression]
25+
default-features = false
26+
features = ["stream", "gzip", "zlib", "brotli", "zstd"]
27+
version = "0.1.0-alpha.1"
28+
29+
[dev-dependencies]
30+
http-service-mock = "0.2.0"

tide-compression/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# tide-compression
2+
3+
This crate provides compression-related middleware for Tide.
4+
5+
## Examples
6+
7+
Examples are in the `/examples` folder of this crate.
8+
9+
__Simple Example__
10+
11+
You can test the simple example by running `cargo run --example simple` while in this crate's directory, and then running either of the following commands:
12+
13+
```console
14+
$ curl http://127.0.0.1:8000/ -v
15+
$ echo 'why hello there' | gzip | curl -v --compressed -H 'Content-Encoding: gzip' 'http://127.0.0.1:8000/echo' --data-binary @-
16+
```

tide-compression/examples/simple.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(async_await)]
2+
use tide::{App, Context};
3+
use tide_compression::{Compression, Decompression, Encoding};
4+
5+
// Returns a portion of the lorem ipsum text.
6+
async fn lorem_ipsum(_cx: Context<()>) -> String {
7+
String::from("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
8+
}
9+
10+
// Echoes the request body in bytes.
11+
async fn echo_bytes(mut cx: Context<()>) -> Vec<u8> {
12+
cx.body_bytes().await.unwrap()
13+
}
14+
15+
pub fn main() {
16+
let mut app = App::new();
17+
app.at("/").get(lorem_ipsum);
18+
app.at("/echo").post(echo_bytes);
19+
app.middleware(Compression::with_default(Encoding::Brotli));
20+
app.middleware(Decompression::new());
21+
app.run("127.0.0.1:8000").unwrap();
22+
}

0 commit comments

Comments
 (0)