关于 rust-fullstack
Rust Full-Stack Web Development Patterns 是面向 Rust Web 应用开发者的技能,涵盖 Leptos 前端、Axum 和 SQLx 后端、PostgreSQL,以及身份验证、状态管理、API 请求和租户路由模式。
主要功能
- 01Leptos 前端搭配 Tailwind CSS 和 DaisyUI
- 02Axum 与 SQLx 后端模式
- 03JWT 访问令牌与刷新令牌流程
- 04前端状态与基于 TTL 的缓存
- 05API 重试处理和租户路由
- 06PostgreSQL 查询与枚举
适用场景
- 01组织 Rust 全栈 Web 应用
- 02实现基于 Cookie 的 JWT 身份验证
- 03处理 API 限流及 401 重试
- 04按租户子域名路由请求
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ daiki48/ dotfiles --skill rust-fullstack安装后在 Claude Code 中输入/rust-fullstack确认已加载。
详细介绍
Rust 全栈 Web 开发模式
技术栈
| 层级 | 技术 |
|---|---|
| 前端 | Leptos(Rust→WASM CSR)、Tailwind CSS + DaisyUI |
| 后端 | Axum、SQLx(编译时检查查询) |
| 数据库 | PostgreSQL |
| 构建 | Trunk(WASM)、Docker、Makefile |
JWT 双令牌认证
Access Token (short-lived) → API request auth
Refresh Token (long-lived) → Access token renewal
- 存储在 HTTP-only Cookie 中(防止 XSS)
- 后端:令牌验证
- 前端:收到 401 时自动重试
// Backend: Cookie setup
let cookie = format!(
"access_token={}; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age={}",
token, 900 // 15 min
);
// Frontend: 401 retry
if response.status() == 401 {
refresh_token().await?;
// Retry original request
}
前端状态管理
provide_context(AuthContext::new());
provide_context(AppCache::new()); // TTL-based cache
provide_context(ToastContext::new());
// localStorage for preferences
let stored = window().local_storage()?.get_item("preferences")?;
API 客户端模式
// Rate limiting (429) handling
async fn fetch_with_retry<T>(request: impl Fn() -> Future<T>) -> Result<T> {
loop {
match request().await {
Err(e) if e.status() == 429 => {
let delay = e.retry_after().unwrap_or(1000);
sleep(delay).await;
}
result => return result,
}
}
}
// Token refresh queue (prevent multiple refreshes)
static REFRESH_LOCK: OnceCell<RwLock<()>> = OnceCell::new();
多租户路由
{tenant}.example.com → Tenant identification
fn extract_subdomain(host: &str) -> Option<String> {
let parts: Vec<&str> = host.split('.').collect();
(parts.len() >= 3).then(|| parts[0].to_string())
}
async fn check_tenant_access(claims: &Claims, subdomain: &str) -> Result<(), AppError> {
// Verify user has access to this tenant
}
数据库
// SQLx compile-time query checking
let user = sqlx::query_as!(
User,
r#"SELECT id, name, role as "role: UserRole" FROM users WHERE id = $1"#,
id
).fetch_one(&pool).await?;
// PostgreSQL ENUMs
#[derive(sqlx::Type)]
#[sqlx(type_name = "user_role", rename_all = "PascalCase")]
pub enum UserRole { Admin, AreaManager, ServiceStation }
常用命令
make dev # Start local environment
make fmt # Format (cargo fmt + leptosfmt)
make lint # Clippy checks
make test # Run tests
make migrate-run # Apply migrations
开发备注
| 项目 | 值 |
|---|---|
| 后端端口 | 3000 |
| 本地子域名 | lvh.me(解析为 127.0.0.1) |
| 环境文件 | .env.development、.env.staging、.env.production |
| 必需环境变量 | DATABASE_URL、JWT_SECRET、APP_ENV |
相关技能
leptos-guide- Leptos 前端axum-guide- Axum 后端sqlx-postgres- SQLx + PostgreSQL
评论
支持 Markdown