Skip to content

Commit 9967e2f

Browse files
authored
Support multiple apps in Plug.SwaggerUI (#676)
1 parent bc1606b commit 9967e2f

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

lib/open_api_spex/plug/swagger_ui.ex

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ defmodule OpenApiSpex.Plug.SwaggerUI do
4141
swagger_ui_js_standalone_preset_url: "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.0/swagger-ui-standalone-preset.js",
4242
swagger_ui_css_url: "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.0/swagger-ui.css"
4343
end
44+
45+
# Multiple paths
46+
scope "/" do
47+
pipe_through :browser # Use the default browser stack
48+
49+
get "/", MyAppWeb.PageController, :index
50+
get "/swaggerui", OpenApiSpex.Plug.SwaggerUI,
51+
paths: [
52+
latest: "/api/openapi",
53+
legacy: "/legacy_api/openapi",
54+
other_app: "http://localhost:4001/my_other_app/api/openapi"
55+
]
56+
end
4457
"""
4558
@behaviour Plug
4659

@@ -91,10 +104,8 @@ defmodule OpenApiSpex.Plug.SwaggerUI do
91104
window.onload = function() {
92105
// Begin Swagger UI call region
93106
const api_spec_url = new URL(window.location);
94-
api_spec_url.pathname = "<%= config.path %>";
95-
api_spec_url.hash = "";
96-
const ui = SwaggerUIBundle({
97-
url: api_spec_url.href,
107+
108+
let swaggerConfig = {
98109
dom_id: '#swagger-ui',
99110
deepLinking: true,
100111
presets: [
@@ -117,7 +128,26 @@ defmodule OpenApiSpex.Plug.SwaggerUI do
117128
<%= for {k, v} <- Map.drop(config, [:path, :oauth, :csp_nonce_assign_key]) do %>
118129
, <%= camelize(k) %>: <%= encode_config(camelize(k), v) %>
119130
<% end %>
120-
})
131+
};
132+
133+
<%= if config[:paths] do %>
134+
let urls = <%=
135+
config[:paths]
136+
|> Enum.map(fn {name, path} -> %{name: name, url: path} end)
137+
|> OpenApiSpex.OpenApi.json_encoder().encode!()
138+
%>
139+
let urlconfig = { urls: urls };
140+
141+
swaggerConfig = { ...swaggerConfig, ...urlconfig};
142+
<% else %>
143+
api_spec_url.pathname = "<%= config.path %>";
144+
api_spec_url.hash = "";
145+
let urlConfig = { url: api_spec_url.href };
146+
147+
swaggerConfig = {...swaggerConfig, ...urlConfig };
148+
<% end %>
149+
const ui = SwaggerUIBundle(swaggerConfig);
150+
121151
// End Swagger UI call region
122152
<%= if config[:oauth] do %>
123153
ui.initOAuth(

0 commit comments

Comments
 (0)