use rweb::*; use serde::Deserialize; #[tokio::main] async fn main() { let (spec, filter) = openapi::spec().build(move || { // Build filters math::math() .or(products::products()) .or(generic::body()) .or(generic::optional()) .or(generic::search()) .or(response::response()) }); println!("go to http://localhost:3030/docs to interact with your openapi!"); serve(filter.or(openapi_docs(spec))) .run(([127, 0, 0, 1], 3030)) .await; } mod response { use rweb::*; use serde::Serialize; #[router("/response", services(json))] pub fn response() {} #[derive(Debug, Serialize, Schema)] pub struct Data { msg: String, } #[get("/json")] pub fn json() -> Json { Json::from(Data { msg: "Hello".into(), }) } } mod math { use rweb::*; #[router("/math", services(sum))] #[openapi(tags("math"))] pub fn math() {} /// Adds a and b /// and /// return it #[get("/sum/{a}/{b}")] fn sum(a: usize, b: usize) -> String { (a + b).to_string() } } mod products { use super::SearchReq; use rweb::*; use serde::{Deserialize, Serialize}; #[derive(Debug, Default, Serialize, Deserialize, Schema)] pub struct Product { pub id: String, pub title: String, } #[router("/products", services(list, product))] #[openapi(tags("products"))] pub fn products() {} #[get("/")] #[openapi(id = "products.list")] #[openapi(summary = "List products")] fn list(_query: Query) -> Json> { // Mix vec![].into() } #[get("/{id}")] #[openapi(id = "products.get")] #[openapi(summary = "Get a product")] #[openapi(response(code = "404", description = "Product not found"))] fn product(id: String) -> Json { Product { id, title: Default::default(), } .into() } } #[derive(Debug, Deserialize, Schema)] struct SearchReq { query: String, } mod generic { use super::SearchReq; use rweb::*; use serde::Deserialize; #[derive(Debug, Deserialize, Schema)] struct LoginForm { id: String, } #[post("/login")] #[openapi(tags("auth"))] pub fn body(_: Json) -> String { String::new() } #[post("/optional")] pub fn optional(_: Option>) -> String { String::new() } #[post("/search")] pub fn search(_: Option>) -> String { String::new() } }