Overview of actix-router
mainactix-router is a library providing resource path matching and routing capabilities. It is designed to handle the logic of mapping incoming request paths to specific handlers or resources.repository·main·Indexed 12 days ago
https://github.com/actix/actix-webA powerful, pragmatic, and high-performance asynchronous web framework for Rust. It supports HTTP/1.x and HTTP/2, WebSockets, and integrates with the awc HTTP client. The ecosystem includes actix-files for static file serving, actix-multipart for handling multipart/form-data, and actix-test for integration testing via TestServer.
actix-router is a library providing resource path matching and routing capabilities. It is designed to handle the logic of mapping incoming request paths to specific handlers or resources.actix-http provides the core HTTP types and services used throughout the Actix ecosystem. It serves as the foundational layer for handling HTTP requests and responses in Actix-based applications.Actix Web is a high-performance web framework with the following core capabilities:
Tokio.awc HTTP client.actix-http-test provides various helper utilities designed to assist developers in testing Actix Web applications. It is intended to be used as a testing dependency to simplify the process of simulating HTTP requests and verifying application behavior.actix-web-codegen provides routing and runtime macros designed for use with Actix Web. These macros help automate the generation of boilerplate code for defining routes and managing the runtime behavior of your web application.The actix-test crate provides integration testing tools for Actix Web applications. The primary tool is TestServer, which spawns a real HTTP server on an unused port and uses a real HTTP client to interact with it.
Using TestServer is preferred for integration tests because it exercises the full HTTP stack (including encoding and decoding), making it more representative of real-world usage compared to actix_web::test::init_service, which bypasses the HTTP layer.
use actix_web::{get, web, test, App, HttpResponse, Error, Responder};
#[get("/")]
async fn my_handler() -> Result<impl Responder, Error> {
Ok(HttpResponse::Ok())
}
#[actix_rt::test]
async fn test_example() {
// Start a real HTTP server on an unused port
let srv = actix_test::start(||
App::new().service(my_handler)
);
// Use the server's client to send a request
let req = srv.get("/");
let res = req.send().await.unwrap();
assert!(res.status().is_success());
}Middleware is a mechanism used to add behavior to the request/response processing lifecycle. It can be used to:
ServiceRequest.Execution Order:
Middleware is registered for each App, Scope, or Resource. It is executed in the reverse order of registration. This means the last middleware you register is the first one to receive the request.
The ResponseBody and Body types have been removed in favor of more expressive components in the body module. Use the following mappings for common requirements:
| Old Type | New Equivalent |
|---|---|
Body::None | body::None::new() |
Body::Empty | () or web::Bytes::new() |
Body::Bytes | web::Bytes::from(...) |
Body::Message | .boxed() or BoxBody |
BoxBody is a type-erased body type. It is useful for handlers or middleware where you want to trade a small amount of performance for simpler types. Create it using .boxed() on a MessageBody type.
EitherBody is a type that implements MessageBody and is useful for middleware that can return different body types (e.g., the inner service's body or a custom error body).
Actix Web provides experimental features for faster iteration. These features are prefixed with experimental and may undergo breaking changes in any release. Use them in production at your own risk.
One available experimental feature is:
experimental-introspection: Exposes route and method reporting helpers for local diagnostics and tooling.The actix-multipart crate provides two ways to handle multipart data:
Multipart (Low-level): A lower-level extractor that supports all multipart/* media types, including multipart/form-data, multipart/related, and multipart/mixed. It allows you to read multipart fields sequentially in the order they are sent by the client.
MultipartForm (High-level): A higher-level extractor and derive macro that is optimized for multipart/form-data specifically. It provides a more ergonomic way to map fields to structured data but does not support other multipart media types.
actix-server has changed. It now defaults to the number of physical CPU cores available, whereas previously it used the number of logical cores. If you experience performance regressions, monitor your CPU core utilization.