Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate embed code instead of using interpolation #828

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ debug = []
debug-embed = ["rust-embed/debug-embed"]

[dependencies]
rust-embed = { version = "8", features = ["interpolate-folder-path"] }
rust-embed = { version = "8" }
mime_guess = { version = "2.0" }
actix-web = { version = "4", optional = true, default-features = false }
rocket = { version = "0.5.0-rc.3", features = ["json"], optional = true }
Expand Down
18 changes: 16 additions & 2 deletions utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ const SWAGGER_UI_DIST_ZIP: &str = "swagger-ui-5.3.1";

fn main() {
println!("cargo:rerun-if-changed=res/{SWAGGER_UI_DIST_ZIP}.zip");
println!("cargo:rustc-env=UTOIPA_SWAGGER_UI_VERSION={SWAGGER_UI_DIST_ZIP}");

let target_dir = env::var("OUT_DIR").unwrap();
println!("cargo:rustc-env=UTOIPA_SWAGGER_DIR={}", &target_dir);

let swagger_ui_zip = File::open(
["res", &format!("{SWAGGER_UI_DIST_ZIP}.zip")]
Expand All @@ -29,6 +27,8 @@ fn main() {
extract_within_path(&mut zip, [SWAGGER_UI_DIST_ZIP, "dist"], &target_dir).unwrap();

replace_default_url_with_config(&target_dir);

write_embed_code(&target_dir, &SWAGGER_UI_DIST_ZIP);
}

fn extract_within_path<const N: usize>(
Expand Down Expand Up @@ -96,3 +96,17 @@ fn replace_default_url_with_config(target_dir: &str) {

fs::write(&path, replaced_swagger_initializer.as_ref()).unwrap();
}

fn write_embed_code(target_dir: &str, swagger_version: &str) {
let contents = format!(
r#"
// This file is auto-generated during compilation, do not modify
#[derive(RustEmbed)]
#[folder = "{}/{}/dist/"]
struct SwaggerUiDist;
"#,
target_dir, swagger_version
);
let path = [target_dir, "embed.rs"].iter().collect::<PathBuf>();
fs::write(&path, &contents).unwrap();
}
4 changes: 1 addition & 3 deletions utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ use serde::Serialize;
#[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))]
use utoipa::openapi::OpenApi;

#[derive(RustEmbed)]
#[folder = "$UTOIPA_SWAGGER_DIR/$UTOIPA_SWAGGER_UI_VERSION/dist/"]
struct SwaggerUiDist;
include!(concat!(env!("OUT_DIR"), "/embed.rs"));

/// Entry point for serving Swagger UI and api docs in application. It provides
/// builder style chainable configuration methods for configuring api doc urls.
Expand Down