Skip to content
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

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
otp_vsn: ['24', '25', '26']
otp_vsn: ['25', '26', '27']
rebar3_vsn: ['3.22']
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
Copy link
Collaborator

@paulo-ferraz-oliveira paulo-ferraz-oliveira Oct 12, 2024

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?

- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp_vsn}}
rebar3-version: ${{matrix.rebar3_vsn}}
- name: Restore _build
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
_build
Expand All @@ -35,7 +35,7 @@ jobs:
-rebar3-${{steps.setup-beam.outputs.rebar3-version}}\
-hash-${{hashFiles('rebar.lock')}}"
- name: Restore rebar3's cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/rebar3
key: "rebar3-cache-for\
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/up_swagger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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\'"
Copy link
Collaborator

@paulo-ferraz-oliveira paulo-ferraz-oliveira Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Are the \" a problem here, that they had to be replaced with \'. If not, the change could be reverted?

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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ erl_crash.dump
doc/
.rebar3
logs
rebar3
/priv/swagger/swagger-initializer.js
cowboy_swagger_hook.beam
vkatsuba marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says the default is /api-docs but we seem to be changing that. Do we need to change the default? I'm missing something, as I don't understand this particular change.

%% `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,
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I don't see why the base path (which is api-docs for all the projects I use this with) would be different from what it was. Can we restore it?

}
}
]
Expand Down
2 changes: 1 addition & 1 deletion priv/swagger/swagger-initializer.js → priv/swagger/swagger-initializer.template
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator

@paulo-ferraz-oliveira paulo-ferraz-oliveira Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Same comment as before, for the ". Was it required to be changed?

dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand Down
41 changes: 41 additions & 0 deletions priv/update_swagger_url
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.
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

{deps, [{jsx, "3.1.0"}, {cowboy, "2.10.0"}, {ranch, "2.1.0"}, {trails, "2.3.1"}]}.

{pre_hooks, [{compile, "priv/update_swagger_url"}]}.
vkatsuba marked this conversation as resolved.
Show resolved Hide resolved

{project_plugins,
[{rebar3_hank, "~> 1.4.0"},
{rebar3_hex, "~> 7.0.7"},
Expand Down
7 changes: 4 additions & 3 deletions src/cowboy_swagger_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ trails(Options) ->
_ ->
filename:join(cowboy_swagger_priv(), "swagger")
end,
SwaggerPath = application:get_env(cowboy_swagger, path, "/api-docs"),
Redirect =
trails:trail("/api-docs",
trails:trail(SwaggerPath,
cowboy_swagger_redirect_handler,
{file, StaticFiles ++ "/index.html"},
#{get => #{hidden => true}}),
Static =
trails:trail("/api-docs/[...]",
trails:trail(SwaggerPath ++ "/[...]",
cowboy_static,
{dir, StaticFiles, [{mimetypes, cow_mimetypes, all}]},
#{get => #{hidden => true}}),
MD = #{get => #{hidden => true}},
Handler =
trails:trail("/api-docs/swagger.json", cowboy_swagger_json_handler, Options, MD),
trails:trail(SwaggerPath ++ "/swagger.json", cowboy_swagger_json_handler, Options, MD),
[Redirect, Handler, Static].

%% @private
Expand Down
3 changes: 2 additions & 1 deletion src/cowboy_swagger_redirect_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 index.html part… HA! 🤦🏻

Now I see why you wanted to use path instead of the full url.
I don't think those 2 things should be necessarily related, @vkatsuba
I would allow the user to configure the swagger base path (which I would call base_path or root_path, instead of path) on a parameter, and the swagger.json location (which I would call swagger_json_url or swagger_url) with another parameter.

Copy link
Author

Choose a reason for hiding this comment

The 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 path config. The application:get_env/3 should be able to get config from top project where it used. Still need to do end to end tests but we mostly there... So, we can prepare different variables now for use them in URL.

Copy link
Member

Choose a reason for hiding this comment

The 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?
I'm pretty sure it won't work as we expect… because the configuration for the cowboy_swagger app can be in whatever path… and thus it may not be visible to your new script.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll write an inline comment in the proper place…

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... end to end testing will be need to do...