Anansi 是一个用于 Rust 的简单 MVC Web 框架。
1、安全第一
除了用 Rust 编写之外,Anansi 还为常见的 Web 安全漏洞提供了防御。
2、高效
Anansi 还允许 Web 应用程序以 Rust 的速度异步运行。
3、 容易上手
Anansi 处理 Web 开发的许多重复部分,让您可以更快地处理应用程序的重要部分。
模型
通过Rust而不是SQL操作数据库:
// A topic in a forum.
#[model]
#[derive(ToUrl)]
pub struct Topic {
pub title: VarChar<200>,
pub date: DateTime,
}
// A comment in a topic.
#[model]
pub struct Comment {
pub topic: ForeignKey<Topic>,
pub user: ForeignKey<auth::models::User>,
pub content: VarChar<40000>,
pub created: DateTime,
}
|
视图
#[viewer]
impl<R: Request> TopicView<R> {
// A view of the last 25 topics.
#[view(if_guest)]
async fn index(req: R) -> Result<Response> {
let title = "Latest Topics";
let topics = Topic::order_by(date().desc())
.limit(25).query(&req).await?;
}
}
|
将请求映射到视图很简单:
routes! {
path("", TopicView::index),
path("new", TopicView::new),
path("{topic_id}", TopicView::show),
}
|
模板
模板能混合Rust 与 HTML 实现格式化。
@extend "base"
@block title {@title}
@block content {
<h1>@title</h1>
<ul>
@for topic in topics {
<li>@link req, Self::show, topic {@topic.title}</li>
}
</ul>
@if req.user().is_auth() {
@link req, Self::new {New Topic}
}
}
|
Form表单
可以毫不费力地创建表单。
#[form(Topic)]
pub struct TopicForm {
pub title: VarChar<200>,
pub content: VarChar<40000>,
}
|
详细点击标题