RustAPI示例
   
生产就绪示例 展示了 RustAPI --具有类似FastAPI开发经验的Rust web框架。
📚 官方资源:
______________________________________________________________________
🚀 为什么选择RustAPI?
*“Rust速度。Python简单。AI效率。”*
| 度量 | RustAPI | Actix | Axum | FastAPI(Python) |
|---|---|---|---|---|
| 演出 | 约92k需求/秒 | 约105k | 约100k | 约12k |
| 开发者体验 | 🟢 高 | 🔴 低 | 🟡 中等 | 🟢 高 |
| 样板文件 | 零 | 高 | 中 | 零 |
| AI/LLM原生 | ✅ | ❌ | ❌ | ❌ |
| 类型安全 | ✅ | ✅ | ✅ | ❌ |
主要特点
- 🎯 零沸点板 —
RustApi::auto()自动发现所有路线 - 📝 汽车文档 --OpenAPI/Swagger
/docs开箱即用 - ✅ 内置验证 —
#[validate]带有详细的错误信息 - 🔐 JWT身份验证 --头等舱
JwtLayer和AuthUser提取器 - 🤖 AI优先架构 --TOON格式(节省50-58%的代币),支持MCP
- ⚡ 中间件堆栈 --速率限制、CORS、日志记录、断路器等
- 🌐 实时 --支持广播通道的WebSocket
______________________________________________________________________
📋 先决条件
在运行这些示例之前,请确保您已经:
# Rust 1.70 or later
rustc --version
# Clone the examples repository
git clone https://github.com/Tuntii/rustapi-rs-examples.git
cd rustapi-rs-examples
# Build all examples
cargo build --release可选依赖关系 (具体示例):
- 码头工人 --For
sqlx-crud(PostgreSQL/SQLite) - SQLite --For
sqlx-crud本地测试
______________________________________________________________________
📂 示例概述
此存储库包含 18个生产就绪示例 按类别组织:
🌟 入门指南
| 示例 | 难度 | 描述 | 主要特征 |
|---|---|---|---|
| 你好,世界 | ⭐ | 最少20行API | RustApi::auto()、路径参数, Json 回应 |
| 积垢api | ⭐⭐ | 完成CRUD操作 | 验证、分页、错误处理、正文限制 |
| 概念验证 | ⭐⭐⭐ | 全功能书签管理器 | JWT、CRUD、SSE、模块化处理程序、Swagger UI |
🔐 身份验证和安全
| 示例 | 难度 | 描述 | 主要特征 |
|---|---|---|---|
| 身份验证api | ⭐⭐⭐ | JWT认证系统 | 登录/注册, JwtLayer, AuthUser,受保护的路线 |
| 利率限制演示 | ⭐⭐ | 基于IP的速率限制 | 每端点限制,突发支持,429处理 |
| 中间件链 | ⭐⭐⭐ | 自定义中间件组合 | 请求ID、定时、身份验证、中间件排序 |
| cors试验 | ⭐⭐ | CORS配置 | CorsLayer,允许的起源/方法/标头 |
🗄️ 数据库集成
🤖 AI和LLM集成
🌐 实时和网络
🏗️ 高级体系结构
| 示例 | 难度 | 描述 | 主要特征 |
|---|---|---|---|
| graphqlapi | ⭐⭐⭐⭐ | GraphQL集成 | 异步GraphQL,查询/突变,游乐场 |
| 微服务 | ⭐⭐⭐⭐ | API网关模式 | 服务到服务通信、路由 |
| 微服务高级 | ⭐⭐⭐⭐ | 服务发现 | 注册表、心跳、Docker Compose |
| 第11阶段演示 | ⭐⭐⭐⭐ | 高级中间件 | 防护、断路器、超时、日志记录 |
| 无服务器lambda | ⭐⭐⭐ | AWS Lambda部署 | SAM模板,冷启动优化 |
⚠️ 备注: serverless-lambda 使用AWS Lambda HTTP运行时而不是RustAPI进行无服务器部署模式。______________________________________________________________________
🎯 功能覆盖矩阵
| 功能 | 使用示例 |
|---|---|
RustApi::auto() | 所有示例 |
Json / JsonResponse | crud-api、auth-api、概念证明、graphql-api |
#[validate] | crud-api、auth-api、概念验证 |
JwtLayer / AuthUser | auth-api、中间件链、phase11演示、概念验证 |
RateLimitLayer | 速率限制演示、auth-api、cors测试、概念验证 |
CorsLayer | cors测试、中间件链、概念验证 |
ToonResponse | toon api、mcp服务器 |
WebSocket / WsConnection | websocket |
View / ViewEngine | 模板 |
State | 所有具有共享状态的示例 |
RequestIdLayer | 中间件链,第11阶段演示 |
CircuitBreakerLayer | 第11阶段演示 |
TimeoutLayer | 第11阶段演示 |
______________________________________________________________________
📊 RustAPI功能标志
每个示例都使用特定的Cargo特征标志。看 功能.md 详细文档。
| 功能标志 | 目的 | 示例 |
|---|---|---|
full | 所有功能已启用 | crud api,第11阶段演示 |
jwt | JWT身份验证 | auth-api、中间件链、概念验证 |
cors | CORS中间件 | CORS测试、中间件链、概念验证 |
rate-limit | 速率限制 | 速率限制演示、auth-api、cors测试、概念验证 |
toon | LLM的TOON格式 | TOON api,mcp服务器 |
ws | WebSocket支持 | WebSocket |
view | 模板渲染 | 模板 |
swagger-ui | Swagger UI位于/docs | toon api |
______________________________________________________________________
🚀 快速开始
运行示例
# Navigate to repository
cd rustapi-rs-examples
# Run any example
cargo run -p hello-world
# Run with debug logging
RUST_LOG=debug cargo run -p crud-api
# Access Swagger documentation (when running)
# http://127.0.0.1:8080/docs测试API端点
# Hello World
curl http://127.0.0.1:8080/
curl http://127.0.0.1:8080/hello/RustAPI
# CRUD API
curl http://127.0.0.1:8080/users
curl -X POST http://127.0.0.1:8080/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "email": "alice@example.com"}'
# Auth API
curl -X POST http://127.0.0.1:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "password"}'______________________________________________________________________
📚 学习路径
要获得结构化的学习体验,请按照我们推荐的进度进行 学习路径.md:
1. hello-world → Basic routing, responses
2. crud-api → Validation, error handling, state management
3. auth-api → JWT authentication, protected routes
4. middleware-chain → Custom middleware, composition
5. proof-of-concept → Full application architecture______________________________________________________________________
🔗 食谱交叉引用
每个示例都映射到 RustAPI食谱:
| 示例 | 食谱部分 |
|---|---|
| 你好,世界 | 入门指南→ 快速入门 |
| 积垢api | 食谱→ CRUD资源 |
| 身份验证api | 食谱→ JWT身份验证 |
| 中间件链 | 食谱→ 自定义中间件 |
| sqlx积垢 | 食谱→ 数据库集成 |
| websocket | 食谱→ WebSocket |
| 模板 | 板条箱→ rustapi视图 |
| toon-api | 板条箱→ 鲁斯塔皮卡通 |
______________________________________________________________________
📝 创造自己的榜样
- 创建目录:
my-example/
- 添加Cargo.toml:
[package]
name = "my-example"
version = "0.1.0"
edition = "2021"
[dependencies]
rustapi-rs = { path = "../../crates/rustapi-rs", features = ["full"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }- 创建src/main.rs:
use rustapi_rs::prelude::*;
#[rustapi_rs::get("/")]
async fn index() -> &'static str {
"Hello from my example!"
}
#[tokio::main]
async fn main() -> Result> {
RustApi::auto().run("127.0.0.1:8080").await
}- 运行它:
cargo run -p my-example______________________________________________________________________
🎯 路线图
即将推出
- \[ \] redis缓存 --Redis缓存层
- \[ \] sse活动 --服务器发送事件流
- \[ \] grpc集成 -gRPC+REST混合API
- \[ \] 分布式追踪 --OpenTetry集成
- \[ \] kubernetes就绪 --健康检查、指标、优雅关机
______________________________________________________________________
🤝 贡献
欢迎投稿!查看我们的 贡献指南 了解详情。
- 分叉此存储库
- 在新目录中创建示例
- 添加全面的文档(README.md)
- 提交拉取请求
______________________________________________________________________
📄 许可证
该项目的许可证为MIT OR Apache-2.0许可证,与 RustAPI.
______________________________________________________________________
建于❤️ 使用 RustAPI
