-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add possibility to configure own path to "/api-docs/swagger.json" #207
base: master
Are you sure you want to change the base?
Changes from 11 commits
cb4054f
588fc25
37fa2f0
50fbcf9
8477453
4848277
32a1770
d9d20c5
357243d
4ea4eaa
a9b01ce
d3dd7c1
0e1e3a5
7d27098
531d223
4a7ed61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ rm -rf node_modules | |
|
||
# Same as https://github.com/swagger-api/swagger-ui/blob/63ad6f6a5bce19075e717ea74acaf9f7055dcdf5/docker/docker-entrypoint.d/40-swagger-ui.sh#L12 | ||
FIND="\"https://petstore.swagger.io/v2/swagger.json\"" | ||
REPLACE="window.location.origin + \"/api-docs/swagger.json\"" | ||
REPLACE="window.location.origin + __SWAGGER_PATH__ + \'/swagger.json\'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Are the |
||
sed -i -e "s|${FIND}|${REPLACE}|g" priv/swagger/swagger-initializer.js | ||
vkatsuba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
echo "${NEW_SWAGGER_VSN}" >SWAGGER_VSN | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,8 @@ Additionally, `cowboy_swagger` can be configured/customized from a `*.config` fi | |
%% cowboy_swagger config | ||
{cowboy_swagger, | ||
[ | ||
%% `path`: Path where you can access Swagger-UI. Default: `/api-docs` | ||
{path, "/api-docs-swagger"}, | ||
Comment on lines
+122
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It says the default is |
||
%% `static_files`: Static content directory. This is where Swagger-UI | ||
%% is located. Default: `priv/swagger`. | ||
%% Remember that Swagger-UI is embedded into `cowboy-swagger` project, | ||
|
@@ -133,7 +135,8 @@ Additionally, `cowboy_swagger` can be configured/customized from a `*.config` fi | |
{global_spec, | ||
#{swagger => "2.0", | ||
info => #{title => "Example API"}, | ||
basePath => "/api-docs" | ||
%% See "path" config section | ||
basePath => "/api-docs-swagger" | ||
Comment on lines
-136
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, I don't see why the base path (which is |
||
} | ||
} | ||
] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ window.onload = function() { | |
|
||
// the following lines will be replaced by docker/configurator, when it runs in a docker-container | ||
window.ui = SwaggerUIBundle({ | ||
url: window.location.origin + "/api-docs/swagger.json", | ||
url: window.location.origin + __SWAGGER_PATH__ + '/swagger.json', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Same comment as before, for the |
||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#! /usr/bin/env escript | ||
|
||
-module(update_swagger_url). | ||
|
||
-export([main/1]). | ||
vkatsuba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
main(_) -> | ||
%% Define the path to the template and the output file | ||
TemplateFilePath = "priv/swagger/swagger-initializer.template", | ||
OutputFilePath = "priv/swagger/swagger-initializer.js", | ||
|
||
%% Read the contents of the template file | ||
case file:read_file(TemplateFilePath) of | ||
{ok, Content} -> | ||
%% Get the configuration for the path from the application environment | ||
Path = application:get_env(cowboy_swagger, path, "/api-docs"), | ||
|
||
%% Ensure Path is a binary (if it's a list, convert to binary) | ||
BinPath = case is_binary(Path) of | ||
true -> Path; | ||
false -> list_to_binary(Path) | ||
end, | ||
|
||
%% Replace the placeholder in the template file content | ||
%% Ensure both the placeholder and replacement are binaries | ||
ModifiedContent = binary:replace(Content, <<"__SWAGGER_PATH__">>, <<"'", BinPath/binary, "'">>, [global]), | ||
|
||
%% Write the modified content to the new JavaScript file | ||
case file:write_file(OutputFilePath, ModifiedContent) of | ||
ok -> | ||
%% Successfully written, log the updated path | ||
io:format("Swagger path updated to: ~s~n", [Path]); | ||
{error, WriteError} -> | ||
%% Failed to write the new file | ||
io:format("Failed to write swagger-initializer.js: ~p~n", [WriteError]) | ||
end; | ||
|
||
{error, ReadError} -> | ||
%% Failed to read the template file | ||
io:format("Failed to read swagger-initializer.template: ~p~n", [ReadError]) | ||
end. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,5 @@ previously_existed(Req, State) -> | |
-spec moved_permanently(Req :: cowboy_req:req(), State :: state()) -> | ||
{{true, iodata()}, cowboy_req:req(), state()}. | ||
moved_permanently(Req, State) -> | ||
{{true, "/api-docs/index.html"}, Req, State}. | ||
SwaggerPath = application:get_env(cowboy_swagger, path, "/api-docs"), | ||
{{true, SwaggerPath ++ "/index.html"}, Req, State}. | ||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooooooh! So this is alredy generating issues in our production systems… nobody relized that because everybody just clicked a link that arleady contained the Now I see why you wanted to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes... make sense... So, now I suppose the code should be prepared as well for use at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooooh… right… that's an issue. Have you tested your changes on another project using this one as a dependency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll write an inline comment in the proper place… There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes... end to end testing will be need to do... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is Ok but shouldn't be required for this PR in particular, right?