domain-web by zhanghandong/rust-skills
npx skills add https://github.com/zhanghandong/rust-skills --skill domain-web第 3 层:领域约束
| 领域规则 | 设计约束 | Rust 实现含义 |
|---|---|---|
| 无状态 HTTP | 无请求级全局变量 | 状态置于提取器中 |
| 并发性 | 处理大量连接 | 异步,Send + Sync |
| 延迟 SLA | 快速响应 | 高效的所有权管理 |
| 安全性 | 输入验证 | 类型安全的提取器 |
| 可观测性 | 请求追踪 | tracing + tower 中间件层 |
RULE: Web 处理程序不得阻塞
WHY: 阻塞一个任务 = 阻塞多个请求
RUST: async/await,CPU 密集型工作使用 spawn_blocking
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RULE: 共享状态必须是线程安全的
WHY: 处理程序可在任意线程上运行
RUST: Arc<T>,可变状态使用 Arc<RwLock<T>>
RULE: 资源仅在请求持续期间存活
WHY: 内存管理,避免泄漏
RUST: 提取器,正确的所有权管理
从约束到设计(第 2 层):
"需要共享应用状态"
↓ m07-concurrency: 使用 Arc 进行线程安全共享
↓ m02-resource: 可变状态使用 Arc<RwLock<T>>
"需要请求验证"
↓ m05-type-driven: 验证型提取器
↓ m06-error-handling: 错误处理使用 IntoResponse
"需要中间件栈"
↓ m12-lifecycle: Tower 中间件层
↓ m04-zero-cost: 基于 Trait 的组合
| 框架 | 风格 | 最佳适用场景 |
|---|---|---|
| axum | 函数式,tower | 现代 API |
| actix-web | 基于 Actor 模型 | 高性能 |
| warp | 过滤器组合 | 可组合 API |
| rocket | 宏驱动 | 快速开发 |
| 用途 | 库 |
|---|---|
| HTTP 服务器 | axum, actix-web |
| HTTP 客户端 | reqwest |
| JSON | serde_json |
| 认证/JWT | jsonwebtoken |
| 会话 | tower-sessions |
| 数据库 | sqlx, diesel |
| 中间件 | tower |
| 模式 | 目的 | 实现 |
|---|---|---|
| 提取器 | 请求解析 | State(db), Json(payload) |
| 错误响应 | 统一错误处理 | impl IntoResponse |
| 中间件 | 横切关注点 | Tower 中间件层 |
| 共享状态 | 应用配置 | Arc<AppState> |
async fn handler(
State(db): State<Arc<DbPool>>,
Json(payload): Json<CreateUser>,
) -> Result<Json<User>, AppError> {
let user = db.create_user(&payload).await?;
Ok(Json(user))
}
// 错误处理
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, message) = match self {
Self::NotFound => (StatusCode::NOT_FOUND, "未找到"),
Self::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "内部错误"),
};
(status, Json(json!({"error": message}))).into_response()
}
}
| 错误 | 违反的领域规则 | 修复方法 |
|---|---|---|
| 在处理程序中阻塞 | 延迟激增 | 使用 spawn_blocking |
| 在状态中使用 Rc | 非 Send + Sync | 使用 Arc |
| 无验证 | 安全风险 | 使用类型安全的提取器 |
| 无错误响应 | 糟糕的用户体验 | 实现 IntoResponse |
| 约束 | 第 2 层模式 | 第 1 层实现 |
|---|---|---|
| 异步处理程序 | Async/await | tokio 运行时 |
| 线程安全状态 | 共享状态 | Arc, Arc<RwLock> |
| 请求生命周期 | 提取器 | 通过 From 实现所有权 |
| 中间件 | Tower 中间件层 | 基于 Trait 的组合 |
| 时机 | 参见 |
|---|---|
| 异步模式 | m07-concurrency |
| 状态管理 | m02-resource |
| 错误处理 | m06-error-handling |
| 中间件设计 | m12-lifecycle |
每周安装量
507
代码仓库
GitHub 星标数
912
首次出现
Jan 20, 2026
安全审计
安装于
opencode464
codex448
gemini-cli441
github-copilot428
amp387
kimi-cli383
Layer 3: Domain Constraints
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| Stateless HTTP | No request-local globals | State in extractors |
| Concurrency | Handle many connections | Async, Send + Sync |
| Latency SLA | Fast response | Efficient ownership |
| Security | Input validation | Type-safe extractors |
| Observability | Request tracing | tracing + tower layers |
RULE: Web handlers must not block
WHY: Block one task = block many requests
RUST: async/await, spawn_blocking for CPU work
RULE: Shared state must be thread-safe
WHY: Handlers run on any thread
RUST: Arc<T>, Arc<RwLock<T>> for mutable
RULE: Resources live only for request duration
WHY: Memory management, no leaks
RUST: Extractors, proper ownership
From constraints to design (Layer 2):
"Need shared application state"
↓ m07-concurrency: Use Arc for thread-safe sharing
↓ m02-resource: Arc<RwLock<T>> for mutable state
"Need request validation"
↓ m05-type-driven: Validated extractors
↓ m06-error-handling: IntoResponse for errors
"Need middleware stack"
↓ m12-lifecycle: Tower layers
↓ m04-zero-cost: Trait-based composition
| Framework | Style | Best For |
|---|---|---|
| axum | Functional, tower | Modern APIs |
| actix-web | Actor-based | High performance |
| warp | Filter composition | Composable APIs |
| rocket | Macro-driven | Rapid development |
| Purpose | Crate |
|---|---|
| HTTP server | axum, actix-web |
| HTTP client | reqwest |
| JSON | serde_json |
| Auth/JWT | jsonwebtoken |
| Session | tower-sessions |
| Database | sqlx, diesel |
| Middleware | tower |
| Pattern | Purpose | Implementation |
|---|---|---|
| Extractors | Request parsing | State(db), Json(payload) |
| Error response | Unified errors | impl IntoResponse |
| Middleware | Cross-cutting | Tower layers |
| Shared state | App config | Arc<AppState> |
async fn handler(
State(db): State<Arc<DbPool>>,
Json(payload): Json<CreateUser>,
) -> Result<Json<User>, AppError> {
let user = db.create_user(&payload).await?;
Ok(Json(user))
}
// Error handling
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, message) = match self {
Self::NotFound => (StatusCode::NOT_FOUND, "Not found"),
Self::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Internal error"),
};
(status, Json(json!({"error": message}))).into_response()
}
}
| Mistake | Domain Violation | Fix |
|---|---|---|
| Blocking in handler | Latency spike | spawn_blocking |
| Rc in state | Not Send + Sync | Use Arc |
| No validation | Security risk | Type-safe extractors |
| No error response | Bad UX | IntoResponse impl |
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Async handlers | Async/await | tokio runtime |
| Thread-safe state | Shared state | Arc, Arc<RwLock> |
| Request lifecycle | Extractors | Ownership via From |
| Middleware | Tower layers | Trait-based composition |
| When | See |
|---|---|
| Async patterns | m07-concurrency |
| State management | m02-resource |
| Error handling | m06-error-handling |
| Middleware design | m12-lifecycle |
Weekly Installs
507
Repository
GitHub Stars
912
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode464
codex448
gemini-cli441
github-copilot428
amp387
kimi-cli383
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装