emqx_authz_http_test_server.erl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authz_http_test_server).
  17. -behaviour(supervisor).
  18. -behaviour(cowboy_handler).
  19. % cowboy_server callbacks
  20. -export([init/2]).
  21. % supervisor callbacks
  22. -export([init/1]).
  23. % API
  24. -export([start_link/2,
  25. stop/0,
  26. set_handler/1
  27. ]).
  28. %%------------------------------------------------------------------------------
  29. %% API
  30. %%------------------------------------------------------------------------------
  31. start_link(Port, Path) ->
  32. supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path]).
  33. stop() ->
  34. gen_server:stop(?MODULE).
  35. set_handler(F) when is_function(F, 2) ->
  36. true = ets:insert(?MODULE, {handler, F}),
  37. ok.
  38. %%------------------------------------------------------------------------------
  39. %% supervisor API
  40. %%------------------------------------------------------------------------------
  41. init([Port, Path]) ->
  42. Dispatch = cowboy_router:compile(
  43. [
  44. {'_', [{Path, ?MODULE, []}]}
  45. ]),
  46. TransOpts = #{socket_opts => [{port, Port}],
  47. connection_type => supervisor},
  48. ProtoOpts = #{env => #{dispatch => Dispatch}},
  49. Tab = ets:new(?MODULE, [set, named_table, public]),
  50. ets:insert(Tab, {handler, fun default_handler/2}),
  51. ChildSpec = ranch:child_spec(?MODULE, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts),
  52. {ok, {{one_for_one, 10, 10}, [ChildSpec]}}.
  53. %%------------------------------------------------------------------------------
  54. %% cowboy_server API
  55. %%------------------------------------------------------------------------------
  56. init(Req, State) ->
  57. [{handler, Handler}] = ets:lookup(?MODULE, handler),
  58. Handler(Req, State).
  59. %%------------------------------------------------------------------------------
  60. %% Internal functions
  61. %%------------------------------------------------------------------------------
  62. default_handler(Req0, State) ->
  63. Req = cowboy_req:reply(
  64. 400,
  65. #{<<"content-type">> => <<"text/plain">>},
  66. <<"">>,
  67. Req0),
  68. {ok, Req, State}.