#swagger-ui #open-api #axum #routes #forms

swagger-ui-dist

packages the JS/CSS code of the swagger-ui in the form of axum routes

6 releases (stable)

5.18.3-preview Jan 1, 2025
5.18.2 Dec 4, 2024
5.17.14 May 28, 2024

#1102 in HTTP server

Download history 140/week @ 2024-09-23 71/week @ 2024-09-30 32/week @ 2024-10-07 141/week @ 2024-10-14 74/week @ 2024-10-21 85/week @ 2024-10-28 107/week @ 2024-11-04 46/week @ 2024-11-11 52/week @ 2024-11-18 57/week @ 2024-11-25 274/week @ 2024-12-02 141/week @ 2024-12-09 124/week @ 2024-12-16 209/week @ 2024-12-23 227/week @ 2024-12-30 108/week @ 2025-01-06

704 downloads per month

Apache-2.0

4MB
130 lines

Latest Version

The version number reflects the swagger-ui version embedded.

Usage

With Inline OpenAPI

use axum::Router;
use swagger_ui_dist::{ApiDefinition, OpenApiSource};

#[tokio::main]
async fn main() {
    let api_def = ApiDefinition {
        uri_prefix: "/api",
        api_definition: OpenApiSource::Inline(include_str!("petstore.yaml")),
        title: Some("My Super Duper API"),
    };
    let app = Router::new().merge(swagger_ui_dist::generate_routes(api_def));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    println!("listening on http://localhost:3000/api");
    axum::serve(listener, app).await.unwrap();
}

With external Route

use axum::{routing::get, Router};
use swagger_ui_dist::{ApiDefinition, OpenApiSource};

#[tokio::main]
async fn main() {
    let api_def = ApiDefinition {
        uri_prefix: "/api",
        api_definition: OpenApiSource::Uri("/openapi.yml"),
        title: Some("My Super Duper API"),
    };
    let app = Router::new()
        .route("/openapi.yml", get(|| async move { include_str!("petstore.yaml") }))
        .merge(swagger_ui_dist::generate_routes(api_def));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    println!("listening on http://localhost:3000/api");
    axum::serve(listener, app).await.unwrap();
}

Supporting axum 0.7 and 0.8

Since axum 0.8 hase breaking changes, the crate supports both axum 0.7 and 0.8. By default, the crate uses the latest axum.

To use axum 0.7, add the following to your Cargo.toml:

[dependencies]
swagger-ui-dist = { version = "5.18.2", default-features = false, features = ["with-axum-07"] }

To use axum 0.8, add the following to your Cargo.toml:

[dependencies]
swagger-ui-dist = { version = "5.18.2" }

Dependencies

~5–12MB
~123K SLoC