-
Notifications
You must be signed in to change notification settings - Fork 174
/
swagger_ui.ex
206 lines (180 loc) · 5.93 KB
/
swagger_ui.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
defmodule OpenApiSpex.Plug.SwaggerUI do
@moduledoc """
Module plug that serves SwaggerUI.
The full path to the API spec must be given as a plug option.
The API spec should be served at the given path, see `OpenApiSpex.Plug.RenderSpec`
## Configuring SwaggerUI
SwaggerUI can be configured through plug `opts`.
All options will be converted from `snake_case` to `camelCase` and forwarded to the `SwaggerUIBundle` constructor.
See the [swagger-ui configuration docs](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) for details.
Should dynamic configuration be required, the `config_url` option can be set to an API endpoint that will provide additional config.
## Example
scope "/" do
pipe_through :browser # Use the default browser stack
get "/", MyAppWeb.PageController, :index
get "/swaggerui", OpenApiSpex.Plug.SwaggerUI,
path: "/api/openapi",
default_model_expand_depth: 3,
display_operation_id: true
end
# Other scopes may use custom stacks.
scope "/api" do
pipe_through :api
resources "/users", MyAppWeb.UserController, only: [:index, :create, :show]
get "/openapi", OpenApiSpex.Plug.RenderSpec, :show
end
"""
@behaviour Plug
@html """
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.0/swagger-ui.css" >
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.0/swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.0/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const api_spec_url = new URL(window.location);
api_spec_url.pathname = "<%= config.path %>";
api_spec_url.hash = "";
const ui = SwaggerUIBundle({
url: api_spec_url.href,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
requestInterceptor: function(request){
server_base = window.location.protocol + "//" + window.location.host;
if(request.url.startsWith(server_base)) {
request.headers["x-csrf-token"] = "<%= csrf_token %>";
} else {
delete request.headers["x-csrf-token"];
}
return request;
}
<%= for {k, v} <- Map.drop(config, [:path, :oauth]) do %>
, <%= camelize(k) %>: <%= encode_config(camelize(k), v) %>
<% end %>
})
// End Swagger UI call region
<%= if config[:oauth] do %>
ui.initOAuth(
<%= config.oauth
|> Map.new(fn {k, v} -> {camelize(k), v} end)
|> OpenApiSpex.OpenApi.json_encoder().encode!()
%>
)
<% end %>
window.ui = ui
}
</script>
</body>
</html>
"""
@ui_config_methods [
"operationsSorter",
"tagsSorter",
"onComplete",
"requestInterceptor",
"responseInterceptor",
"modelPropertyMacro",
"parameterMacro",
"initOAuth",
"preauthorizeBasic",
"preauthorizeApiKey"
]
@doc """
Initializes the plug.
## Options
* `:path` - Required. The URL path to the API definition.
* `:oauth` - Optional. Config to pass to the `SwaggerUIBundle.initOAuth()` function.
* all other opts - forwarded to the `SwaggerUIBundle` constructor
## Example
get "/swaggerui", OpenApiSpex.Plug.SwaggerUI,
path: "/api/openapi",
default_model_expand_depth: 3,
display_operation_id: true
"""
@impl Plug
def init(opts) when is_list(opts) do
Map.new(opts)
end
@impl Plug
def call(conn, config) do
csrf_token = Plug.CSRFProtection.get_csrf_token()
config = supplement_config(config, conn)
html = render(config, csrf_token)
conn
|> Plug.Conn.put_resp_content_type("text/html")
|> Plug.Conn.send_resp(200, html)
end
require EEx
EEx.function_from_string(:defp, :render, @html, [
:config,
:csrf_token
])
defp camelize(identifier) do
identifier
|> to_string
|> String.split("_", parts: 2)
|> case do
[first] -> first
[first, rest] -> first <> Macro.camelize(rest)
end
end
defp encode_config("tagsSorter", "alpha" = value) do
OpenApiSpex.OpenApi.json_encoder().encode!(value)
end
defp encode_config("operationsSorter", value) when value == "alpha" or value == "method" do
OpenApiSpex.OpenApi.json_encoder().encode!(value)
end
defp encode_config(key, value) do
case Enum.member?(@ui_config_methods, key) do
true -> value
false -> OpenApiSpex.OpenApi.json_encoder().encode!(value)
end
end
if Code.ensure_loaded?(Phoenix.Controller) do
defp supplement_config(%{oauth2_redirect_url: {:endpoint_url, path}} = config, conn) do
endpoint_module = Phoenix.Controller.endpoint_module(conn)
url = Path.join(endpoint_module.url(), path)
Map.put(config, :oauth2_redirect_url, url)
end
end
defp supplement_config(config, _conn) do
config
end
end