Yet another web server framework for rust.
Installation (without automatic openapi generation):
[dependencies]
rweb = "0.4"
tokio = "0.2"
- Safe & Correct
Since rweb
is based on warp, which features safety and correctness, rweb
has same property.
- Easy to read code
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)]
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)]
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
If you want to use websocket, just declare a parameter typed Ws
. It's all.
use rweb::*;
#[get("/ws")]
fn example(ws: ws::Ws) -> String {
String::new("use ws.on_upgrade or extra")
}
- Automatic openapi spec generation
rweb supports automatically generating openapi specification file based on your code.
See: docuementation for usage.