From cb4054f7c40e203da9ccea78ba38eca23f268a14 Mon Sep 17 00:00:00 2001 From: vk Date: Wed, 9 Oct 2024 10:17:03 +0300 Subject: [PATCH] Add possibility to configure own path to "/api-docs/swagger.json" --- .github/workflows/up_swagger.sh | 2 +- .gitignore | 2 ++ README.md | 5 +++- priv/swagger/swagger-initializer.js | 2 +- rebar.config.script | 32 +++++++++++++++++++++++++ src/cowboy_swagger_handler.erl | 7 +++--- src/cowboy_swagger_redirect_handler.erl | 3 ++- 7 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 rebar.config.script diff --git a/.github/workflows/up_swagger.sh b/.github/workflows/up_swagger.sh index 0ad7dc1..e5a05cf 100755 --- a/.github/workflows/up_swagger.sh +++ b/.github/workflows/up_swagger.sh @@ -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 diff --git a/.gitignore b/.gitignore index ec92b13..e0c889a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ erl_crash.dump doc/ .rebar3 logs +rebar3 +priv/swagger/swagger-initializer.js diff --git a/README.md b/README.md index 3953d77..7f8a40c 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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" } } ] diff --git a/priv/swagger/swagger-initializer.js b/priv/swagger/swagger-initializer.js index f593a39..5921302 100644 --- a/priv/swagger/swagger-initializer.js +++ b/priv/swagger/swagger-initializer.js @@ -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: [ diff --git a/rebar.config.script b/rebar.config.script new file mode 100644 index 0000000..25c1cb8 --- /dev/null +++ b/rebar.config.script @@ -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]), + CONFIG; + {error, Reason} -> + io:format("Failed to read swagger-initializer.js: ~p~n", [Reason]), + CONFIG +end. diff --git a/src/cowboy_swagger_handler.erl b/src/cowboy_swagger_handler.erl index 2691bad..21eb060 100644 --- a/src/cowboy_swagger_handler.erl +++ b/src/cowboy_swagger_handler.erl @@ -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 diff --git a/src/cowboy_swagger_redirect_handler.erl b/src/cowboy_swagger_redirect_handler.erl index 92e2bf4..0985d27 100644 --- a/src/cowboy_swagger_redirect_handler.erl +++ b/src/cowboy_swagger_redirect_handler.erl @@ -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}.