11 releases (4 stable)

Uses new Rust 2021

2.0.1 Sep 2, 2022
2.0.0 Aug 30, 2022
1.1.0 Jun 10, 2022
1.0.0 Apr 27, 2022
0.1.0-beta1 Mar 7, 2022

#100 in Web programming

Download history 784/week @ 2022-06-11 783/week @ 2022-06-18 927/week @ 2022-06-25 1136/week @ 2022-07-02 962/week @ 2022-07-09 1073/week @ 2022-07-16 1001/week @ 2022-07-23 1971/week @ 2022-07-30 2386/week @ 2022-08-06 1755/week @ 2022-08-13 1274/week @ 2022-08-20 2132/week @ 2022-08-27 4186/week @ 2022-09-03 5574/week @ 2022-09-10 4670/week @ 2022-09-17 3794/week @ 2022-09-24

18,696 downloads per month
Used in cookbook

MIT/Apache

4MB
4.5K SLoC

utoipa-swagger-ui

Utoipa build crates.io docs.rs rustc

This crate implements necessary boiler plate code to serve Swagger UI via web server. It works as a bridge for serving the OpenAPI documetation created with utoipa libarary in the Swagger UI.

Currently implemented boiler plate for:

  • actix-web version >= 4
  • rocket version >=0.5.0-rc.1
  • axum version >=0.5

Serving Swagger UI is framework independant thus this crate also supports serving the Swagger UI with other frameworks as well. With other frameworks there is bit more manual implementation to be done. See more details at serve or examples.

Features

  • actix-web Enables actix-web integration with pre-configured SwaggerUI service factory allowing users to use the Swagger UI without a hazzle.
  • rocket Enables rocket integration with with pre-configured routes for serving the Swagger UI and api doc without a hazzle.
  • axum Enables axum integration with pre-configured Router serving Swagger UI and OpenAPI specs hazzle free.
  • debug-embed Enables debug-embed feature on rust_embed crate to allow embedding files in debug builds as well.

Install

Use only the raw types without any boiler plate implementation.

[dependencies]
utoipa-swagger-ui = "2"

Enable actix-web framework with Swagger UI you could define the dependency as follows.

[dependencies]
utoipa-swagger-ui = { version = "2", features = ["actix-web"] }

Note! Also remember that you already have defined utoipa dependency in your Cargo.toml

Examples

Serve Swagger UI with api doc via actix-web. See full example from examples.

HttpServer::new(move || {
    App::new()
        .service(
            SwaggerUi::new("/swagger-ui/{_:.*}")
                .url("/api-doc/openapi.json", ApiDoc::openapi()),
        )
  })
  .bind((Ipv4Addr::UNSPECIFIED, 8989)).unwrap()
  .run();

Serve Swagger UI with api doc via rocket. See full example from examples.

#[rocket::launch]
fn rocket() -> Rocket<Build> {
    rocket::build()
        .mount(
            "/",
            SwaggerUi::new("/swagger-ui/<_..>")
                .url("/api-doc/openapi.json", ApiDoc::openapi()),
        )
}

Setup Router to serve Swagger UI with axum framework. See full implementation of how to serve Swagger UI with axum from examples.

let app = Router::new()
    .merge(SwaggerUi::new("/swagger-ui/*tail")
        .url("/api-doc/openapi.json", ApiDoc::openapi()));

License

Licensed under either of Apache 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be dual licensed, without any additional terms or conditions.

Dependencies

~4–40MB
~720K SLoC