API的Rust网络框架. 很简单,感觉就像作弊.
Rapina
A Rust web framework for APIs. So simple it feels like cheating.
use rapina::prelude::*;
#[get("/users")]
async fn list_users(db: Db) -> Result>> {
let users = User::find_all(db.conn()).await?;
Ok(Json(users))
}
#[post("/users")]
async fn create_user(input: Validated>, db: Db) -> Result> {
let user = User::create(db.conn(), input.into_inner()).await?;
Ok(Json(user))
}
#[tokio::main]
async fn main() -> std::io::Result {
Rapina::new()
.discover()
.listen("127.0.0.1:3000")
.await
}No router configuration. No manual wiring. Annotate your handlers, call .discover(), ship.
cargo install rapina-cli
rapina new my-app
cd my-app
rapina devYour API is running. That's it.
Auto-discovery — annotate handlers with #[get], #[post], #[put], #[delete]. Rapina finds them at startup. Your main.rs stays three lines.
Database from day one — define your schema declaratively. author: User becomes a foreign key. posts: Vec becomes a relationship. SeaORM entities are auto-generated.
schema! {
User {
name: String,
email: String,
posts: Vec
}
Post {
title: String,
body: String,
author: User
}
}Auth that works — JWT authentication, protected by default. Mark public routes with #[public]. Access the current user with the CurrentUser extractor.
#[public]
#[post("/login")]
async fn login(body: Json, auth: State) -> Result> {
let token = auth.create_token(&body.username)?;
Ok(Json(TokenResponse::new(token, auth.expiration())))
}
#[get("/me")]
async fn me(user: CurrentUser) -> Json {
Json(UserResponse { id: user.id })
}CRUD in one command — scaffold an entire resource with handlers, DTOs, errors, schema, and migration.
rapina add resource PostOpenAPI built-in — spec generation, breaking change detection, and validation from the CLI.
rapina openapi export -o openapi.json
rapina openapi diff --base mainProduction middleware — rate limiting, compression, CORS out of the box. Prometheus metrics available with the metrics feature flag.
Rapina::new()
.with_rate_limit(RateLimitConfig::per_minute(100))
.with_compression(CompressionConfig::default())
.with_cors(CorsConfig::permissive())
.with_metrics(true) // Requires `metrics` feature flag
.discover()
.listen("0.0.0.0:3000")
.awaitCLI for everything — create, develop, test, inspect, generate.
rapina new my-app # Scaffold a new project
rapina dev # Dev server with hot reload
rapina test --coverage # Tests with coverage report
rapina test --watch # Watch mode
rapina routes # List all routes
rapina doctor # Health checks and diagnostics
rapina migrate new # Create a migration
rapina add resource Post # Full CRUD scaffoldingEverything you need to pick apart a request, type-safe and compile-time checked.
Json · Path · Query · Headers · Cookie · State · CurrentUser · Db · ... and more
See the extractors documentation for the full list.
Every error includes a trace_id. No more guessing in production.
{
"error": { "code": "NOT_FOUND", "message": "user not found" },
"trace_id": "550e8400-e29b-41d4-a716-446655440000"
}Rapina is in active development with a rapid release cadence. The API is stabilizing but breaking changes may still occur before 1.0.0.
See the roadmap for what's coming next.
Full documentation at userapina.com
Contributions are welcome. Check out the open issues or join us on Discord.
MIT
暂无开放 Issues,或尚未同步最近议题。