Actix-Web
actix-web is a framework for writting web applications and REST APIs.
Installation
# To install:
cargo add actix-web
# Recommended for validation
cargo add validator actix-web-validator
Simple example
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.app_data(here_you_can_add_dependencies)
.service(your_handler_functions)
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}
Custom extractors
Extractors are used in the parameter lists of your service functions. You can write own to "extract" informations from a request (e.g. get a user by authorization header). To write such a custom header, you need to create a struct (which will be used in the parameter list and contain the extracted information) and then implmenet the FromRequest trait:
struct MyExtractor {
...
}
impl FromRequest for MyExtractor {
type Error = actix_web::Error;
type Future = future_utils::LocalBoxFuture<'static, Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let req = req.clone();
Box::pin(async move {
// Do extraction...
Ok(MyExtractor { ... })
})
}
}