Skip to content

Commit 272ceb8

Browse files
authored
Make SWAGGER_UI_DOWNLOAD_URL support file:// urls (#923)
1 parent 9f8ebf3 commit 272ceb8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

utoipa-swagger-ui/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ The following configuration env variables are available at build time:
5555

5656
* `SWAGGER_UI_DOWNLOAD_URL`:
5757

58-
* the url from where to download the swagger-ui zip file
58+
* the url from where to download the swagger-ui zip file if starts with `http://` or `https://`
59+
* the file path from where to copy the swagger-ui zip file if starts with `file://`
5960
* default value: <https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.3.zip>
60-
* All versions: <https://github.com/swagger-api/swagger-ui/tags>
61+
* all versions: <https://github.com/swagger-api/swagger-ui/tags>
6162

6263
* `SWAGGER_UI_OVERWRITE_FOLDER`:
6364

64-
* absolute path to a folder containing files to overwrite the default swagger-ui files
65+
* absolute path to a folder containing files to overwrite the swagger-ui files extracted from the `.zip` file
6566
* typically you might want to overwrite `index.html`
6667

6768
## Examples

utoipa-swagger-ui/build.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use zip::{result::ZipError, ZipArchive};
1111

1212
/// the following env variables control the build process:
1313
/// 1. SWAGGER_UI_DOWNLOAD_URL:
14-
/// + the url from where to download the swagger-ui zip file
14+
/// + the url from where to download the swagger-ui zip file if starts with http:// or https://
15+
/// + the file path from where to copy the swagger-ui zip file if starts with file://
1516
/// + default value is SWAGGER_UI_DOWNLOAD_URL_DEFAULT
1617
/// + for other versions, check https://github.com/swagger-api/swagger-ui/tags
1718
/// 2. SWAGGER_UI_OVERWRITE_FOLDER
@@ -32,10 +33,18 @@ fn main() {
3233
let zip_path = [&target_dir, &zip_filename].iter().collect::<PathBuf>();
3334

3435
if !zip_path.exists() {
35-
println!("start download to : {:?}", zip_path);
36-
download_file(&url, zip_path.clone()).unwrap();
36+
if url.starts_with("http://") || url.starts_with("https://") {
37+
println!("start download to : {:?}", zip_path);
38+
download_file(&url, zip_path.clone()).unwrap();
39+
} else if url.starts_with("file://") {
40+
let file_path = url.replace("file://", "");
41+
println!("start copy to : {:?}", zip_path);
42+
fs::copy(file_path, zip_path.clone()).unwrap();
43+
} else {
44+
panic!("invalid SWAGGER_UI_DOWNLOAD_URL: {} -> must start with http:// | https:// | file://", url);
45+
}
3746
} else {
38-
println!("already downloaded: {:?}", zip_path);
47+
println!("already downloaded or copied: {:?}", zip_path);
3948
}
4049

4150
println!("cargo:rerun-if-changed={:?}", zip_path.clone());

0 commit comments

Comments
 (0)