-
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
[Fix #38] Add redirect for /api-docs ... #41
Changes from 1 commit
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 |
---|---|---|
|
@@ -92,18 +92,19 @@ validate_metadata(Metadata) -> | |
-spec filter_cowboy_swagger_handler([trails:trail()]) -> [trails:trail()]. | ||
filter_cowboy_swagger_handler(Trails) -> | ||
F = fun(Trail) -> | ||
case trails:handler(Trail) of | ||
cowboy_swagger_handler -> false; | ||
cowboy_static -> false; | ||
_ -> true | ||
end | ||
MD = trails:metadata(Trail), | ||
maps:size(maps:filter(fun is_visible/2, MD)) /= 0 | ||
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. According to Dialyzer:
|
||
end, | ||
lists:filter(F, Trails). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Private API. | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @private | ||
is_visible(_Method, Metadata) -> | ||
not maps:get(hidden, Metadata, false). | ||
|
||
%% @private | ||
swagger_paths([], Acc) -> | ||
Acc; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
%%% @doc Cowboy Swagger Handler. This handler exposes a GET operation | ||
%%% to enable that `swagger.json' can be retrieved from embedded | ||
%%% Swagger-UI (located in `priv/swagger' folder). | ||
-module(cowboy_swagger_json_handler). | ||
|
||
%% Cowboy callbacks | ||
-export([ init/3 | ||
, rest_init/2 | ||
, content_types_provided/2 | ||
]). | ||
|
||
%% Handlers | ||
-export([handle_get/2]). | ||
|
||
-type state() :: #{}. | ||
-type route_match() :: '_' | iodata(). | ||
-type options() :: #{server => ranch:ref(), host => route_match()}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Cowboy Callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @hidden | ||
-spec init({atom(), atom()}, cowboy_req:req(), options()) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
init(_Transport, _Req, _Opts) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
|
||
%% @hidden | ||
-spec rest_init(cowboy_req:req(), options()) -> | ||
{ok, cowboy_req:req(), options()}. | ||
rest_init(Req, Opts) -> | ||
{ok, Req, Opts}. | ||
|
||
%% @hidden | ||
-spec content_types_provided(cowboy_req:req(), state()) -> | ||
{[term()], cowboy_req:req(), state()}. | ||
content_types_provided(Req, State) -> | ||
{[{<<"application/json">>, handle_get}], Req, State}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Handlers | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @hidden | ||
handle_get(Req, State) -> | ||
Server = maps:get(server, State, '_'), | ||
HostMatch = maps:get(host, State, '_'), | ||
Trails = trails:all(Server, HostMatch), | ||
{cowboy_swagger:to_json(Trails), Req, State}. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-module(cowboy_swagger_redirect_handler). | ||
|
||
%% Cowboy callbacks | ||
-export([ init/3 | ||
, rest_init/2 | ||
, content_types_provided/2 | ||
]). | ||
|
||
%% Handlers | ||
-export([resource_exists/2, previously_existed/2, moved_permanently/2]). | ||
|
||
-type state() :: #{}. | ||
-type route_match() :: '_' | iodata(). | ||
-type options() :: #{server => ranch:ref(), host => route_match()}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Cowboy Callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @hidden | ||
-spec init({atom(), atom()}, cowboy_req:req(), options()) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
init(_Transport, _Req, _Opts) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
|
||
%% @hidden | ||
-spec rest_init(cowboy_req:req(), options()) -> | ||
{ok, cowboy_req:req(), options()}. | ||
rest_init(Req, Opts) -> | ||
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. You can safely remove this function |
||
{ok, Req, Opts}. | ||
|
||
%% @hidden | ||
-spec content_types_provided(cowboy_req:req(), state()) -> | ||
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. This function is probably useless. Can you try removing it to see if everything still works, please? |
||
{[term()], cowboy_req:req(), state()}. | ||
content_types_provided(Req, State) -> | ||
{[{<<"application/json">>, handle_get}], Req, State}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Handlers | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% @hidden | ||
-spec resource_exists(Req::cowboy_req:req(), State::state()) -> | ||
{boolean(), cowboy_req:req(), state()}. | ||
resource_exists(Req, State) -> | ||
{false, Req, State}. | ||
|
||
%% @hidden | ||
-spec previously_existed(Req::cowboy_req:req(), State::state())-> | ||
{boolean(), cowboy_req:req(), state()}. | ||
previously_existed(Req, State) -> | ||
{true, Req, State}. | ||
|
||
%% @hidden | ||
-spec moved_permanently(Req::cowboy_req:req(), State::state()) -> | ||
{{boolean(), string()}, cowboy_req:req(), state()}. | ||
moved_permanently(Req, State) -> | ||
{{true, "/api-docs/index.html"}, Req, State}. |
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.
According to Xref:
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.
@gadgetci you really should start using Erlang/OTP 18.x