mirror of https://github.com/fafhrd91/actix-web
Compare commits
6 Commits
463b4b5844
...
fb4704595a
Author | SHA1 | Date |
---|---|---|
|
fb4704595a | |
|
8996198f2c | |
|
68624ec63b | |
|
bcd0ffb016 | |
|
97526fbe9e | |
|
e247c3bcc4 |
|
@ -17,7 +17,6 @@ edition.workspace = true
|
||||||
rust-version.workspace = true
|
rust-version.workspace = true
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
features = [
|
features = [
|
||||||
"http2",
|
"http2",
|
||||||
"ws",
|
"ws",
|
||||||
|
@ -119,7 +118,7 @@ tokio-util = { version = "0.7", features = ["io", "codec"] }
|
||||||
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
tracing = { version = "0.1.30", default-features = false, features = ["log"] }
|
||||||
|
|
||||||
# http2
|
# http2
|
||||||
h2 = { version = "0.3.26", optional = true }
|
h2 = { version = "0.3.27", optional = true }
|
||||||
|
|
||||||
# websockets
|
# websockets
|
||||||
base64 = { version = "0.22", optional = true }
|
base64 = { version = "0.22", optional = true }
|
||||||
|
|
|
@ -11,7 +11,6 @@ edition.workspace = true
|
||||||
rust-version.workspace = true
|
rust-version.workspace = true
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -14,7 +14,6 @@ license.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
||||||
[package.metadata.cargo_check_external_types]
|
[package.metadata.cargo_check_external_types]
|
||||||
|
|
|
@ -17,7 +17,6 @@ edition.workspace = true
|
||||||
rust-version.workspace = true
|
rust-version.workspace = true
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
features = [
|
features = [
|
||||||
"macros",
|
"macros",
|
||||||
"openssl",
|
"openssl",
|
||||||
|
|
|
@ -2,16 +2,80 @@
|
||||||
|
|
||||||
## What Is A Middleware?
|
## What Is A Middleware?
|
||||||
|
|
||||||
|
Middleware in Actix Web is a powerful mechanism that allows you to add additional behavior to request/response processing. It enables you to:
|
||||||
|
|
||||||
|
- Pre-process incoming requests (e.g., path normalization, authentication)
|
||||||
|
- Post-process outgoing responses (e.g., logging, compression)
|
||||||
|
- Modify application state through ServiceRequest
|
||||||
|
- Access external services (e.g., sessions, caching)
|
||||||
|
|
||||||
|
Middleware is registered for each App, Scope, or Resource and executed in the reverse order of registration. This means the last registered middleware is the first to process the request.
|
||||||
|
|
||||||
## Middleware Traits
|
## Middleware Traits
|
||||||
|
|
||||||
|
Actix Web's middleware system is built on two main traits:
|
||||||
|
|
||||||
|
1. `Transform<S, Req>`: The builder trait that creates the actual Service. It's responsible for:
|
||||||
|
|
||||||
|
- Creating new middleware instances
|
||||||
|
- Assembling the middleware chain
|
||||||
|
- Handling initialization errors
|
||||||
|
|
||||||
|
2. `Service<Req>`: The trait that represents the actual middleware functionality. It:
|
||||||
|
- Processes requests and responses
|
||||||
|
- Can modify both request and response
|
||||||
|
- Can short-circuit request processing
|
||||||
|
- Must be implemented for the middleware to work
|
||||||
|
|
||||||
## Understanding Body Types
|
## Understanding Body Types
|
||||||
|
|
||||||
|
When working with middleware, it's important to understand body types:
|
||||||
|
|
||||||
|
- Middleware can work with different body types for requests and responses
|
||||||
|
- The `MessageBody` trait is used to handle different body types
|
||||||
|
- You can use `EitherBody` when you need to handle multiple body types
|
||||||
|
- Be careful with body consumption - once a body is consumed, it cannot be read again
|
||||||
|
|
||||||
## Best Practices
|
## Best Practices
|
||||||
|
|
||||||
|
1. Keep middleware focused and single-purpose
|
||||||
|
2. Handle errors appropriately and propagate them correctly
|
||||||
|
3. Be mindful of performance impact
|
||||||
|
4. Use appropriate body types and handle them correctly
|
||||||
|
5. Consider middleware ordering carefully
|
||||||
|
6. Document your middleware's behavior and requirements
|
||||||
|
7. Test your middleware thoroughly
|
||||||
|
|
||||||
## Error Propagation
|
## Error Propagation
|
||||||
|
|
||||||
|
Proper error handling is crucial in middleware:
|
||||||
|
|
||||||
|
1. Always propagate errors from the inner service
|
||||||
|
2. Use appropriate error types
|
||||||
|
3. Handle initialization errors
|
||||||
|
4. Consider using custom error types for specific middleware errors
|
||||||
|
5. Document error conditions and handling
|
||||||
|
|
||||||
## When To (Not) Use Middleware
|
## When To (Not) Use Middleware
|
||||||
|
|
||||||
|
Use middleware when you need to:
|
||||||
|
|
||||||
|
- Add cross-cutting concerns
|
||||||
|
- Modify requests/responses globally
|
||||||
|
- Add authentication/authorization
|
||||||
|
- Add logging or monitoring
|
||||||
|
- Handle compression or caching
|
||||||
|
|
||||||
|
Avoid middleware when:
|
||||||
|
|
||||||
|
- The functionality is specific to a single route
|
||||||
|
- The operation is better handled by a service
|
||||||
|
- The overhead would be too high
|
||||||
|
- The functionality can be implemented more simply
|
||||||
|
|
||||||
## Author's References
|
## Author's References
|
||||||
|
|
||||||
- `EitherBody` + when is middleware appropriate: https://discord.com/channels/771444961383153695/952016890723729428
|
- `EitherBody` + when is middleware appropriate: https://discord.com/channels/771444961383153695/952016890723729428
|
||||||
|
- Actix Web Documentation: https://docs.rs/actix-web
|
||||||
|
- Service Trait Documentation: https://docs.rs/actix-service
|
||||||
|
- MessageBody Trait Documentation: https://docs.rs/actix-web/latest/actix_web/body/trait.MessageBody.html
|
||||||
|
|
|
@ -16,7 +16,6 @@ license = "MIT OR Apache-2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
features = [
|
features = [
|
||||||
"cookies",
|
"cookies",
|
||||||
"openssl",
|
"openssl",
|
||||||
|
@ -109,7 +108,7 @@ cfg-if = "1"
|
||||||
derive_more = { version = "2", features = ["display", "error", "from"] }
|
derive_more = { version = "2", features = ["display", "error", "from"] }
|
||||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||||
futures-util = { version = "0.3.17", default-features = false, features = ["alloc", "sink"] }
|
futures-util = { version = "0.3.17", default-features = false, features = ["alloc", "sink"] }
|
||||||
h2 = "0.3.26"
|
h2 = "0.3.27"
|
||||||
http = "0.2.7"
|
http = "0.2.7"
|
||||||
itoa = "1"
|
itoa = "1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# developed on macOS and probably doesn't work on Linux yet due to minor
|
# developed on macOS and probably doesn't work on Linux yet due to minor
|
||||||
# differences in flags on sed
|
# differences in flags on sed
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# run tests matching what CI does for non-linux feature sets
|
|
||||||
|
|
||||||
set -x
|
|
||||||
|
|
||||||
EXIT=0
|
|
||||||
|
|
||||||
save_exit_code() {
|
|
||||||
eval $@
|
|
||||||
local CMD_EXIT=$?
|
|
||||||
[ "$CMD_EXIT" = "0" ] || EXIT=$CMD_EXIT
|
|
||||||
}
|
|
||||||
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-router --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-http --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-web --features=rustls,openssl -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-web-codegen --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=awc --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-http-test --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-test --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-files -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-multipart --all-features -- --nocapture
|
|
||||||
save_exit_code cargo test --lib --tests -p=actix-web-actors --all-features -- --nocapture
|
|
||||||
|
|
||||||
save_exit_code cargo test --workspace --doc
|
|
||||||
|
|
||||||
if [ "$EXIT" = "0" ]; then
|
|
||||||
PASSED="All tests passed!"
|
|
||||||
|
|
||||||
if [ "$(command -v figlet)" ]; then
|
|
||||||
figlet "$PASSED"
|
|
||||||
else
|
|
||||||
echo "$PASSED"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
exit $EXIT
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -Euo pipefail
|
||||||
|
|
||||||
|
for dir in $@; do
|
||||||
|
cd "$dir"
|
||||||
|
|
||||||
|
cargo publish --dry-run
|
||||||
|
|
||||||
|
read -p "Look okay? "
|
||||||
|
read -p "Sure? "
|
||||||
|
|
||||||
|
cargo publish
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo
|
||||||
|
read -p "Was the above error caused by cyclic dev-deps? Choosing yes will publish without a git backreference. (y/N) " publish_no_dev_deps
|
||||||
|
|
||||||
|
if [[ "$publish_no_dev_deps" == "y" || "$publish_no_dev_deps" == "Y" ]]; then
|
||||||
|
cargo hack --no-dev-deps publish --allow-dirty
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd ..
|
||||||
|
done
|
Loading…
Reference in New Issue