1 个不稳定版本
| 0.1.0 | 2020 年 1 月 17 日 | 
|---|
#766 in HTTP 服务器
2KB
rweb
又一个 Rust 的 Web 服务器框架。
安装
[dependencies]
rweb = "0.6"
tokio = "1"
安装(带有自动 openapi 生成)
[dependencies]
rweb = { version = "0.6", features = ["openapi"] }
serde = "1"
tokio = "1"
特性
- 安全且正确
由于 rweb 基于 warp,它具有安全性和正确性,因此 rweb 也具有相同的特性。
- 易于阅读的代码
use rweb::*;
use serde::{Serialize, Deserialize};
#[get("/output")]
fn output() -> String {
    String::from("this returns 200 with text/plain mime type")
}
#[derive(Debug, Serialize, Deserialize, Schema)]
struct Product {
    id: String,
    title: String,
}
#[get("/products")]
fn products() -> Json<Vec<Product>> {
    // ...
    // This returns 200 with application/json
}
#[get("/products/{id}")]
fn product(id: String) -> Json<Product> {
    // ...
    // This returns 200 with application/json
}
#[get("/product")]
fn new_product(_product: Json<Product>) -> Json<Product> {
    // ...
    // This returns 200 with application/json
}
#[derive(Debug, Serialize, Deserialize, Schema)]
struct SearchOption {
    query: String,
    limit: usize,
    page_token: String,
}
#[get("/search")]
fn search(_product: Query<SearchOption>) -> Json<Vec<Product>> {
    // ...
    // This returns 200 with application/json
}
#[tokio::main]
async fn main() {
    serve(output().or(product()).or(products()).or(search())).run(([127, 0, 0, 1], 3030)).await;
}
- WebSocket
如果您想使用 WebSocket,只需声明一个类型为 Ws 的参数即可。就这样。
use rweb::*;
#[get("/ws")]
fn example(ws: ws::Ws) -> String {
    String::new("use ws.on_upgrade or extra")
}
- 自动生成 openapi 规范文件
rweb 支持根据您的代码自动生成 openapi 规范文件。
有关使用方法,请参阅文档。
- 为 openapi 的 API UX 交互
// Build openapi for your API
let (spec, filter) = openapi::spec().build(move || {
    // Your API's filters
    math::math()
        .or(products::products())
        .or(generic::body())
        .or(generic::optional())
        .or(generic::search())
        .or(response::response())
});
println!("go to https://:3030/docs to interact with your openapi!");
serve(filter.or(openapi_docs(spec)))
    .run(([127, 0, 0, 1], 3030))
    .await;
比较
| 名称 | rweb | actix-web | gotham | iron | nickel | rocket | rouille | Thruster | Tide | tower-web | warp | 
|---|---|---|---|---|---|---|---|---|---|---|---|
| 许可证 | |||||||||||
| 版本 | |||||||||||
| 最近下载 | |||||||||||
| GitHub 星标 | |||||||||||
| 贡献者 | |||||||||||
| 活动 | |||||||||||
| 基础框架 | hyper / warp | tokio | hyper | hyper | hyper | hyper | tiny-http | tokio | hyper | hyper | hyper | 
| https | Y | Y | Y | ? | ? | ? | ? | ? | ? | ? | Y | 
| http 2 | Y | Y | ? | ? | ? | ? | ? | ? | ? | ? | Y | 
| 异步 | Y | Y | Y | Y | Y | Y | Y(通过不同的方法) | ||||
| 稳定的 Rust | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | |
| openapi 支持 | Y |