Expand description
RustAPI: FastAPI-inspired REST framework for Rust
RustAPI brings the developer experience of FastAPI and NestJS to Rust, with automatic OpenAPI generation, built-in validation, and dependency injection.
§Features
- Route Macros: Define endpoints with
#[get],#[post], etc. The HTTP verb from the annotation is a binding contract — enforced at registration time via themount_handlers!macro. - Kleisli Pipeline:
RouterPipelinecomposesControllerKleisli arrows withand_then(>>=), short-circuiting on any error. - Direct DI: Pass
Arc<Service>directly topipeline.mount::<C>(svc). No type-map registry required for the primary use case. - Type-Driven: Leverage Rust’s type system for validation and docs.
- Zero-Cost: Built on Axum and Tokio for production performance.
§Quick Start
ⓘ
use rust_api::prelude::*;
pub struct UserController;
#[get("/users")]
pub async fn list_users(State(svc): State<Arc<UserService>>) -> Json<Vec<User>> {
Json(svc.list())
}
mount_handlers!(UserController, UserService, [(__list_users_route, list_users)]);
#[tokio::main]
async fn main() {
let svc = Arc::new(UserService::new());
let app = RouterPipeline::new()
.mount::<UserController>(svc)
.map(|r| r.layer(TraceLayer::new_for_http()))
.build()
.unwrap();
RustAPI::new(app).port(3000).serve().await.unwrap();
}Re-exports§
pub use app::App;pub use controller::Controller;pub use di::Container;pub use di::Injectable;pub use error::Error;pub use error::Result;pub use middleware::guard;pub use middleware::require_bearer;pub use pipeline::RouterPipeline;pub use pipeline::RouterTransform;pub use router::method_filter_from_str;pub use router::ApiRoute;pub use router::Router;pub use router::RouterExt;pub use server::RustAPI;
Modules§
- app
- Application builder for rust-api framework
- controller
- Controller trait — pure descriptor for composable route groups.
- di
- Dependency Injection Container (optional utility)
- error
- Error types for rust-api framework
- header
- HTTP header types
- middleware
- Request-lifecycle middleware utilities and Tower layer factories.
- pipeline
- Monadic router pipeline for composable, error-propagating route registration.
- prelude
- Prelude module for convenient imports.
- router
- Router utilities for RustAPI framework
- routing
- server
- Server runtime for RustAPI framework
Macros§
- mount_
handlers - Generate a
Controllerimplementation for a type from a handler list.
Structs§
- Arc
- A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
- Cors
Layer - Layer that applies the
Corsmiddleware which adds headers for CORS. - Json
- JSON Extractor / Response.
- Parts
- Component parts of an HTTP
Request - Path
- Extractor that will get captures from the URL and parse them using
serde. - Query
- Extractor that deserializes query strings into some type.
- State
- Extractor for state.
- Status
Code - An HTTP status code (
status-codein RFC 9110 et al.). - Trace
Layer Layerthat adds high level tracing to aService.
Traits§
- Deserialize
- A data structure that can be deserialized from any data format supported by Serde.
- From
Request Parts - Types that can be created from request parts.
- Into
Response - Trait for generating responses.
- Serialize
- A data structure that can be serialized into any data format supported by Serde.
Type Aliases§
- Response
- Type alias for
http::Responsewhose body type defaults toBody, the most common body type used with axum.
Attribute Macros§
- delete
- Define a DELETE route handler
- get
- Define a GET route handler
- patch
- Define a PATCH route handler
- post
- Define a POST route handler
- put
- Define a PUT route handler