1个不稳定版本

0.1.0 2020年1月17日

#6#rweb


用于 rweb-testing

Apache-2.0

2KB

rweb

Build Status

又一个基于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://127.0.0.1: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
许可证 license license license license license license license license license license license
版本 version version version version version version version version version version version
最近下载 recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads recent downloads
Github stars github stars github stars github stars github stars github stars github stars github stars github stars github stars github stars github stars
贡献者 contributors contributors contributors contributors contributors contributors contributors contributors contributors contributors contributors
活动 activity activity activity activity activity activity activity activity activity activity activity
基础框架 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

依赖项

~1.5MB
~35K SLoC