5个版本
| 0.1.4 | 2024年5月5日 |
|---|---|
| 0.1.3 | 2024年5月4日 |
| 0.1.2 | 2024年5月4日 |
| 0.1.1 | 2024年5月3日 |
| 0.1.0 | 2024年5月3日 |
#17 在 #htmx
在 ruxt 中使用
16KB
243 代码行
Ruxt宏
这是Ruxt(一个Ruxt Web框架)宏的集合。
安装
运行以下命令将Ruxt添加到您的项目中
cargo add ruxt
将以下内容添加到您的 Cargo.toml
[dependencies]
ruxt-macros = "0.1.4"
用法
#[ruxt_macros::main]
async fn main() -> std::io::Result<()> {
let test_data = "Hello, World!";
HttpServer::new(move || App::new().app_data(test_data.to_string()))
.bind(("0.0.0.0", 8080))?
.run()
.await
}
基本路由
Ruxt的 main 宏会自动为 routes 目录中的文件生成路由。路由的生成基于文件名,因此名为 index.rs 的文件将在服务器的根目录下可用。
宏根据函数名确定要使用哪个HTTP动词。例如,名为 get 的函数将是一个 GET 路由。
例如
// routes/index.rs
use actix_web::{web, HttpResponse, Responder};
pub async fn get() -> impl Responder {
HttpResponse::Ok().body("Hello, World!")
}
将作为GET请求在 https://:8080/ 中可用。
以下动词可用于路由
getpostputpatchdelete
动态路径
可以通过命名文件夹或文件以两个前置下划线开头来创建动态路由。例如,名为 __user 的文件夹将在 /user/{id} 处创建动态路由。
// routes/__user.rs
use actix_web::{web, HttpResponse, Responder};
pub async fn post(id: web::Path<String>) -> impl Responder {
HttpResponse::Ok().body(format!("Hello, {}!", id))
}
将作为POST请求在 https://:8080/user/{id} 中可用。
当前限制
- 截至目前,由于宏生成路由的方式,无法有名为
mod或index的路由。我正在寻找解决方案。
许可证
本项目采用MIT许可证。
依赖项
~17–29MB
~518K SLoC