Skip to content
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

Rework HTTP handler configuration #3636

Merged
merged 20 commits into from
Apr 27, 2022
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d71d8d2
Export process/3 to merge processor functions when merging sections
chrzaszcz Apr 25, 2022
1a4b8ff
Move http handler config specs out of mongoose_config_spec
chrzaszcz Apr 25, 2022
62a55f9
Move merge_sections/2 to mongoose_config_utils
chrzaszcz Apr 25, 2022
0a5ccd0
Remove unused include
chrzaszcz Apr 25, 2022
1706de1
Add a new behaviour: mongoose_http_handler
chrzaszcz Apr 25, 2022
12d72a5
Add mongoose_http_handler callbacks to mongoose_domain_handler
chrzaszcz Apr 25, 2022
a84386c
Update options type in mod_bosh
chrzaszcz Apr 25, 2022
3f70340
Add mongoose_http_handler callbacks to mod_websockets
chrzaszcz Apr 25, 2022
3d4bfd9
Add mongoose_http_handler callbacks to mongoose_api
chrzaszcz Apr 25, 2022
b24ffda
Add mongoose_http_handler callbacks to mongoose_api_admin
chrzaszcz Apr 25, 2022
f45db8d
Add mongoose_http_handler callbacks to mongoose_api_client
chrzaszcz Apr 25, 2022
19a798a
Add mongoose_http_handler callbacks to mongoose_client_api
chrzaszcz Apr 25, 2022
a40f308
Move handler-related functionality out of ejabberd_cowboy
chrzaszcz Apr 25, 2022
f61348a
Update handler collection for system metrics
chrzaszcz Apr 25, 2022
aa71aef
Use the new, simplified format for mongoose_client_api
chrzaszcz Apr 25, 2022
3be89a9
Specify http options in maps in small tests
chrzaszcz Apr 25, 2022
22ebfd7
Update small tests for HTTP handler config options
chrzaszcz Apr 25, 2022
aac7f26
Update big test helpers after the changes in handler options
chrzaszcz Apr 25, 2022
8367450
Update docs for the changed HTTP handlers
chrzaszcz Apr 25, 2022
eeda654
Fix indentation
Apr 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/mongoose_client_api/mongoose_client_api.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
-module(mongoose_client_api).

-behaviour(mongoose_http_handler).

%% mongoose_http_handler callbacks
-export([config_spec/0, routes/1]).

-export([init/2]).
-export([content_types_provided/2]).
-export([is_authorized/2]).
Expand All @@ -16,7 +21,49 @@
options/2, to_json/2]).

-include("mongoose.hrl").
-include("jlib.hrl").
-include("mongoose_config_spec.hrl").

-type handler_options() :: #{path := string(), handlers := [module()], docs := boolean(),
atom() => any()}.

%% mongoose_http_handler callbacks

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
HandlerModules = [Module || {_, Module, _} <- api_paths()],
#section{items = #{<<"handlers">> => #list{items = #option{type = atom,
validate = {enum, HandlerModules}},
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
validate = unique},
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
<<"docs">> => #option{type = boolean}},
defaults = #{<<"handlers">> => HandlerModules,
<<"docs">> => true},
format_items = map}.

-spec routes(handler_options()) -> mongoose_http_handler:routes().
routes(Opts = #{path := BasePath}) ->
[{[BasePath, Path], Module, ModuleOpts}
|| {Path, Module, ModuleOpts} <- api_paths(Opts)] ++ api_doc_paths(Opts).

api_paths(#{handlers := HandlerModules}) ->
lists:filter(fun({_, Module, _}) -> lists:member(Module, HandlerModules) end, api_paths()).

api_paths() ->
[{"/sse", lasse_handler, #{module => mongoose_client_api_sse}},
{"/messages/[:with]", mongoose_client_api_messages, #{}},
{"/contacts/[:jid]", mongoose_client_api_contacts, #{}},
{"/rooms/[:id]", mongoose_client_api_rooms, #{}},
{"/rooms/[:id]/config", mongoose_client_api_rooms_config, #{}},
{"/rooms/:id/users/[:user]", mongoose_client_api_rooms_users, #{}},
{"/rooms/[:id]/messages", mongoose_client_api_rooms_messages, #{}}].

api_doc_paths(#{docs := true}) ->
[{"/api-docs", cowboy_swagger_redirect_handler, #{}},
{"/api-docs/swagger.json", cowboy_swagger_json_handler, #{}},
{"/api-docs/[...]", cowboy_static, {priv_dir, cowboy_swagger, "swagger",
[{mimetypes, cow_mimetypes, all}]}
}];
api_doc_paths(#{docs := false}) ->
[].

init(Req, _Opts) ->
State = #{},
Expand Down