Skip to content

Commit

Permalink
Fix utoipa-swagger-ui cross build
Browse files Browse the repository at this point in the history
* Fix utoipa-swagger-ui build with rust-cross. Previously failed because of missing command from cross docker container. Thus moved to pure Rust implementation allowing applications again be cross built with rust-cross.
* Update swagger-ui version to 4.10.3 the latest available
* Fix swagger-ui urls replace previously was in index.html but since new version was release the the urls configuration has been changed to swagger-initializer.js.
  • Loading branch information
juhaku committed Apr 5, 2022
1 parent 7256c11 commit 8d8e3d6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
5 changes: 4 additions & 1 deletion utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "utoipa-swagger-ui"
description = "Swagger UI for utoipa"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -20,3 +20,6 @@ utoipa = { version = "0.1.0", path = "..", default-features = false, features =

[package.metadata.docs.rs]
features = ["actix-web"]

[build-dependencies]
zip = "0.6"
74 changes: 66 additions & 8 deletions utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use std::{
cmp::Ordering,
env::{self, VarError},
fs::{self, File},
io,
path::PathBuf,
process::Command,
};

const SWAGGER_UI_DIST_ZIP: &str = "swagger-ui-4.6.1";
use zip::{result::ZipError, ZipArchive};

const SWAGGER_UI_DIST_ZIP: &str = "swagger-ui-4.10.3";

fn main() {
println!("cargo:rerun-if-changed=res/{}.zip", SWAGGER_UI_DIST_ZIP);
Expand All @@ -25,19 +31,71 @@ fn main() {
.unwrap();
println!("cargo:rustc-env=UTOIPA_SWAGGER_DIR={}", &target_dir);

Command::new("unzip")
.arg(&format!("res/{}.zip", SWAGGER_UI_DIST_ZIP))
.arg(&format!("{}/dist/**", SWAGGER_UI_DIST_ZIP))
.args(&["-d", &target_dir])
.status()
.unwrap();
let swagger_ui_zip = File::open(
["res", &format!("{}.zip", SWAGGER_UI_DIST_ZIP)]
.iter()
.collect::<PathBuf>(),
)
.unwrap();

let mut zip = ZipArchive::new(swagger_ui_zip).unwrap();
extract_within_path(&mut zip, [SWAGGER_UI_DIST_ZIP, "dist"], &target_dir).unwrap();

Command::new("sed")
.args(&[
"-i",
r#"s|url: ".*",|{{urls}},|"#,
&format!("target/{}/dist/index.html", SWAGGER_UI_DIST_ZIP),
&format!(
"{}/{}/dist/swagger-initializer.js",
&target_dir, SWAGGER_UI_DIST_ZIP
),
])
.status()
.unwrap();
}

fn extract_within_path<const N: usize>(
zip: &mut ZipArchive<File>,
path_segments: [&str; N],
target_dir: &str,
) -> Result<(), ZipError> {
for index in 0..zip.len() {
let mut file = zip.by_index(index)?;
let filepath = file
.enclosed_name()
.ok_or(ZipError::InvalidArchive("invalid path file"))?;

if filepath
.iter()
.take(2)
.map(|s| s.to_str().unwrap_or_default())
.cmp(path_segments)
== Ordering::Equal
{
let directory = [&target_dir].iter().collect::<PathBuf>();
let outpath = directory.join(filepath);

if file.name().ends_with('/') {
fs::create_dir_all(&outpath)?;
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p)?;
}
}
let mut outfile = fs::File::create(&outpath)?;
io::copy(&mut file, &mut outfile)?;
}
// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?;
}
}
}
}

Ok(())
}
Binary file added utoipa-swagger-ui/res/swagger-ui-4.10.3.zip
Binary file not shown.
Binary file removed utoipa-swagger-ui/res/swagger-ui-4.6.1.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async fn serve_swagger_ui(path: web::Path<String>, data: web::Data<Vec<Url<'_>>>
if let Some(file) = SwaggerUiDist::get(&part) {
let mut bytes = file.data.into_owned();

if part == "index.html" {
if part == "swagger-initializer.js" {
let mut index = match String::from_utf8(bytes.to_vec()) {
Ok(index) => index,
Err(error) => return HttpResponse::InternalServerError().body(error.to_string()),
Expand Down

0 comments on commit 8d8e3d6

Please sign in to comment.