Savlo

English   简体中文   繁體中文

build status build status build status
crates.io Documentation unsafe forbidden Rust Version
Website codecov Download License

salvo_oapi allows you to easily implement Apis that comply with the OpenApiv3 specification. It uses procedural macros to generate a lots of boilerplate code, so that you only need to focus on the more important business implementations.

Features

Crate features

To avoid compiling unused dependencies, salvo_oapi gates certain features, some of which are disabled by default:

| Feature | Description | |--------------|----------------------------------------------------------------------------------| | chrono | Integrate with the chrono crate. | | swagger-ui | Add swagger UI support | | email | Support for email address string | | hostname | Support for hostname string | | uuid | Integrate with the uuid crate | | url | Integrate with the url crate | | bson | Integrate with the bson crate | | rust_decimal | Integrate with the rust_decimal crate | | static-files | Support for static file response |

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Example

```rust use salvocore::prelude::*; use salvooapi::extract::QueryParam;

[endpoint]

async fn hello(body: QueryParamOption>, res: &mut Response) { res.render(format!("{:?}", body)) }

[tokio::main]

async fn main() -> Result<(), std::io::Error> { let router = Router::new() .push(Router::with_path("hello").get(hello))

let doc = OpenApi::new(Info::new("Hello API", "0.0.1")).merge_router(&router);

let router = router
    .push(doc.into_router("/api-doc/openapi.json"))
    .push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui"));

let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
Server::new(acceptor).serve(router).await;

} ```

This feature needs to be opted-in. It can be done by adding the feature in Cargo.toml file toml filename=Cargo.toml [dependencies] salvo = { version = "0.39", features = ["oapi"]} tokio = { version = "1", features = ["full"] }

Run example

Open http://localhost:5800/ in your browser, you will see the Swagger UI that contains these Api definitions.

```shell

cargo run --example hello_world

curl http://localhost:5800 hello!

curl http://localhost:5800\?name\=chris hello, chris!
```

Contributing

:balloon: Thanks for your help improving the project! We are so happy to have you!

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in salvo_oapi by you, shall be licensed as Apache, without any additional terms or conditions.