Skip to content

Commit

Permalink
Add possibility to configure own path to "/api-docs/swagger.json"
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed Oct 9, 2024
1 parent 24334d8 commit 4068290
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
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\'"
sed -i -e "s|${FIND}|${REPLACE}|g" priv/swagger/swagger-initializer.js

echo "${NEW_SWAGGER_VSN}" >SWAGGER_VSN
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ erl_crash.dump
doc/
.rebar3
logs
rebar3
priv/swagger/swagger-initializer.js
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"},
%% `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"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion priv/swagger/swagger-initializer.js
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',
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand Down
32 changes: 32 additions & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%% Reading the contents of the file
case file:read_file("priv/swagger/swagger-initializer.js") of
{ok, Content} ->
%% Get the configuration for the path from the application configuration
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,

%% Determine the path to the JavaScript file
FilePath = "priv/swagger/swagger-initializer.js",



%% Replace the placeholder in the file contents
%% Ensure both the placeholder and replacement are binaries
ModifiedContent = binary:replace(Content, <<"__SWAGGER_PATH__">>, <<"\'", BinPath/binary, "\'">>, [global]),

%% Write the updated file back
ok = file:write_file(FilePath, ModifiedContent),

%% Successfully complete hook execution

io:format("Swagger path updated to: ~s~n", [Path]),
[];
{error, Reason} ->
io:format("Failed to read swagger-initializer.js: ~p~n", [Reason]),
[]
end.
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}.

0 comments on commit 4068290

Please sign in to comment.