emqx_authz_http_test_server.erl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 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([
  25. start_link/2,
  26. stop/0,
  27. set_handler/1
  28. ]).
  29. %%------------------------------------------------------------------------------
  30. %% API
  31. %%------------------------------------------------------------------------------
  32. start_link(Port, Path) ->
  33. supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path]).
  34. stop() ->
  35. gen_server:stop(?MODULE).
  36. set_handler(F) when is_function(F, 2) ->
  37. true = ets:insert(?MODULE, {handler, F}),
  38. ok.
  39. %%------------------------------------------------------------------------------
  40. %% supervisor API
  41. %%------------------------------------------------------------------------------
  42. init([Port, Path]) ->
  43. Dispatch = cowboy_router:compile(
  44. [
  45. {'_', [{Path, ?MODULE, []}]}
  46. ]
  47. ),
  48. TransOpts = #{
  49. socket_opts => [{port, Port}],
  50. connection_type => supervisor
  51. },
  52. ProtoOpts = #{env => #{dispatch => Dispatch}},
  53. Tab = ets:new(?MODULE, [set, named_table, public]),
  54. ets:insert(Tab, {handler, fun default_handler/2}),
  55. ChildSpec = ranch:child_spec(?MODULE, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts),
  56. {ok, {{one_for_one, 10, 10}, [ChildSpec]}}.
  57. %%------------------------------------------------------------------------------
  58. %% cowboy_server API
  59. %%------------------------------------------------------------------------------
  60. init(Req, State) ->
  61. [{handler, Handler}] = ets:lookup(?MODULE, handler),
  62. Handler(Req, State).
  63. %%------------------------------------------------------------------------------
  64. %% Internal functions
  65. %%------------------------------------------------------------------------------
  66. default_handler(Req0, State) ->
  67. Req = cowboy_req:reply(
  68. 400,
  69. #{<<"content-type">> => <<"text/plain">>},
  70. <<"">>,
  71. Req0
  72. ),
  73. {ok, Req, State}.