Expand description
§RustAPI
A FastAPI-like web framework for Rust.
RustAPI combines Rust’s performance and safety with FastAPI’s “just write business logic” approach. It provides automatic OpenAPI documentation, declarative validation, and a developer-friendly experience.
§Quick Start
ⓘ
use rustapi_rs::prelude::*;
#[derive(Serialize, Schema)]
struct Hello {
message: String,
}
async fn hello() -> Json<Hello> {
Json(Hello {
message: "Hello, World!".to_string(),
})
}
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
RustApi::new()
.route("/", get(hello))
.run("127.0.0.1:8080")
.await
}§Features
- DX-First: Minimal boilerplate, intuitive API
- Type-Safe: Compile-time route and schema validation
- Auto Documentation: OpenAPI + Swagger UI out of the box
- Declarative Validation: Pydantic-style validation on structs
- Batteries Included: JWT, CORS, rate limiting (optional features)
§Optional Features
Enable these features in your Cargo.toml:
jwt- JWT authentication middleware andAuthUser<T>extractorcors- CORS middleware with builder pattern configurationrate-limit- IP-based rate limiting middlewareconfig- Configuration management with.envfile supportcookies- Cookie parsing extractorsqlx- SQLx database error conversion to ApiErrorextras- Meta feature enabling jwt, cors, and rate-limitfull- All optional features enabled
[dependencies]
rustapi-rs = { version = "0.1", features = ["jwt", "cors"] }Modules§
- auto_
route - Auto-route registration using linkme distributed slices
- auto_
schema - Auto-schema registration using linkme distributed slices
- middleware
- Middleware infrastructure for RustAPI
- multipart
- Multipart form data extractor for file uploads
- path_
validation - Route path validation utilities
- prelude
- Prelude module - import everything you need with
use rustapi_rs::prelude::* - sse
- Server-Sent Events (SSE) response types for RustAPI
- static_
files - Static file serving for RustAPI
- stream
- Streaming response types for RustAPI
Macros§
- route
- Helper macro to create a Route from a handler with RouteHandler trait
Structs§
- ApiError
- Standard API error type
- Body
- Raw body bytes extractor
- Body
Limit Layer - Body size limit middleware layer
- Client
Ip - Client IP address extractor
- Created
- 201 Created response wrapper
- Extension
- Extension extractor
- Handler
Service - Wrapper to convert a Handler into a tower Service
- Header
Value - Single header value extractor
- Headers
- Headers extractor
- Html
- HTML response wrapper
- Json
- JSON body extractor
- Keep
Alive - Keep-alive configuration for SSE connections
- Method
Router - HTTP method router for a single path
- Multipart
- Multipart form data extractor
- Multipart
Config - Configuration for multipart form handling
- Multipart
Field - A single field from a multipart form
- NoContent
- 204 No Content response
- Path
- Path parameter extractor
- Query
- Query string extractor
- Redirect
- Redirect response
- Request
- HTTP Request wrapper
- Request
Id - A unique identifier for a request
- Request
IdLayer - Middleware layer that generates a unique request ID for each request
- Route
- Represents a route definition that can be registered with .mount()
- Router
- Main router
- RustApi
- Main application builder for RustAPI
- Rust
ApiConfig - Configuration builder for RustAPI with auto-routes
- Sse
- Server-Sent Events response wrapper
- SseEvent
- A Server-Sent Event
- State
- State extractor
- Static
File - Static file response
- Static
File Config - Static file serving configuration
- Stream
Body - A streaming body wrapper for HTTP responses
- Tracing
Layer - Middleware layer that creates tracing spans for requests
- Uploaded
File - File data wrapper for convenient access to uploaded files
- Validated
Json - Validated JSON body extractor
- With
Status - Generic wrapper for returning a response with a custom status code.
Enums§
- Environment
- Environment configuration for error handling behavior
Constants§
- DEFAULT_
BODY_ LIMIT - Default body size limit: 1MB
Traits§
- From
Request - Trait for extracting data from the full request (including body)
- From
Request Parts - Trait for extracting data from request parts (headers, path, query)
- Handler
- Trait representing an async handler function
- Into
Response - Trait for types that can be converted into an HTTP response
- Route
Handler - Trait for handlers with route metadata (generated by
#[rustapi::get], etc.)
Functions§
- apply_
auto_ schemas - Apply all auto-registered schemas into the given OpenAPI spec.
- collect_
auto_ routes - Collect all auto-registered routes.
- delete
- Create a DELETE route handler
- delete_
route - Create a DELETE route
- get
- Create a GET route handler
- get_
environment - Get the current environment (cached)
- get_
route - Create a GET route
- patch
- Create a PATCH route handler
- patch_
route - Create a PATCH route
- post
- Create a POST route handler
- post_
route - Create a POST route
- put
- Create a PUT route handler
- put_
route - Create a PUT route
- serve_
dir - Create a static file serving route
- sse_
response - Create an SSE response from a synchronous iterator of events
Type Aliases§
Attribute Macros§
- delete
- DELETE route handler macro
- description
- Description macro for detailed endpoint description in OpenAPI documentation
- get
- GET route handler macro
- main
- Main entry point macro for RustAPI applications
- patch
- PATCH route handler macro
- post
- POST route handler macro
- put
- PUT route handler macro
- schema
- Auto-register a schema type for zero-config OpenAPI.
- summary
- Summary macro for endpoint summary in OpenAPI documentation
- tag
- Tag macro for grouping endpoints in OpenAPI documentation