Skip to content

Commit

Permalink
Refactor swagger config (#169)
Browse files Browse the repository at this point in the history
Refactor existing swagger config. New config gives users accesss to
advanced configuration options of Swagger UI. Newly added options
include every **docker variable** compatible parameter from 
https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md.
  • Loading branch information
juhaku authored Jun 15, 2022
1 parent 25c3dda commit 37b616d
Show file tree
Hide file tree
Showing 5 changed files with 966 additions and 119 deletions.
3 changes: 3 additions & 0 deletions utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ utoipa = { version = "1", path = "..", default-features = false, features = [] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }

[dev-dependencies]
similar = "2.1"

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

Expand Down
8 changes: 4 additions & 4 deletions utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
let mut zip = ZipArchive::new(swagger_ui_zip).unwrap();
extract_within_path(&mut zip, [SWAGGER_UI_DIST_ZIP, "dist"], &target_dir).unwrap();

replace_default_url(&target_dir);
replace_default_url_with_config(&target_dir);
}

fn extract_within_path<const N: usize>(
Expand Down Expand Up @@ -91,9 +91,9 @@ fn extract_within_path<const N: usize>(
Ok(())
}

fn replace_default_url(target_dir: &str) {
fn replace_default_url_with_config(target_dir: &str) {
lazy_static! {
static ref RE: Regex = Regex::new(r#"url: ".*","#).unwrap();
static ref RE: Regex = Regex::new(r#"(?ms)url:.*deep.*true,"#).unwrap();
}

let path = [
Expand All @@ -107,7 +107,7 @@ fn replace_default_url(target_dir: &str) {

let swagger_initializer = fs::read_to_string(&path).unwrap();

let replaced_swagger_initializer = RE.replace(&swagger_initializer, "{{urls}},");
let replaced_swagger_initializer = RE.replace(&swagger_initializer, "{{config}},");

fs::write(&path, replaced_swagger_initializer.as_ref()).unwrap();
}
8 changes: 5 additions & 3 deletions utoipa-swagger-ui/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ impl HttpServiceFactory for SwaggerUi {

let swagger_resource = Resource::new(self.path.as_ref())
.guard(Get())
.app_data(Data::new(Config {
urls,
oauth: self.oauth,
.app_data(Data::new(if let Some(mut config) = self.config {
config.urls = urls;
config
} else {
Config::new(urls)
}))
.to(serve_swagger_ui);

Expand Down
Loading

0 comments on commit 37b616d

Please sign in to comment.