#openapi #routes #actix

swagger-ui-dist

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

10 stable releases

new 5.20.2 Mar 28, 2025
5.20.1 Mar 10, 2025
5.20.0 Feb 27, 2025
5.18.3 Jan 29, 2025
5.17.14 May 28, 2024

#356 in HTTP server

Download history 121/week @ 2024-12-08 115/week @ 2024-12-15 225/week @ 2024-12-22 238/week @ 2024-12-29 121/week @ 2025-01-05 63/week @ 2025-01-12 70/week @ 2025-01-19 249/week @ 2025-01-26 222/week @ 2025-02-02 134/week @ 2025-02-09 395/week @ 2025-02-16 342/week @ 2025-02-23 291/week @ 2025-03-02 386/week @ 2025-03-09 113/week @ 2025-03-16 78/week @ 2025-03-23

896 downloads per month

Apache-2.0

4MB
234 lines

Latest Version

This crate packages the swagger-ui from https://github.com/swagger-api/swagger-ui/. The version number reflects the swagger-ui version embedded.

Usage

This crate can either be used with axum or actix. You can enable/disable the implementations through feature flags.

For Axum 0.8 (which is the default)

Cargo.toml

[dependencies]
swagger-ui-dist = "*"

For Axum 0.7

Cargo.toml

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

For Actix

Cargo.toml

[dependencies]
swagger-ui-dist = { version = "*", default-features = false, features = ["with-actix"] }

Implementation

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();
}

Actix Sample use

use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use swagger_ui_dist::{generate_scope, ApiDefinition, OpenApiSource};

async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let api_def = ApiDefinition {
        uri_prefix: "/api".to_string(),
        api_definition: OpenApiSource::Inline(include_str!("petstore.yaml").to_string()),
        title: Some("My Super Duper API".to_string()),
    };

    println!("listening on http://localhost:8080/api/");

    HttpServer::new(move || {
        App::new()
            .service(web::scope("/").route("", web::get().to(hello)))
            .service(generate_scope(api_def.clone()))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Examples

More examples are available through the examples-directory in the repository.

Dependencies

~0–11MB
~121K SLoC