This repository was archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Dynamic endpoints handlers #10
Open
hdiedrich
wants to merge
7
commits into
apache:master
Choose a base branch
from
hdiedrich:27037-5-dynamic-endpoint-handlers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2fe0c7e
Dynamic endpoints handlers
4a2cb81
Delete inappropriate endpoint tests
4e11fd2
Deleted line breaking ok in tty
e69362f
Add reload test from original hitch app
ecd4f77
Altered and blanketed use of ?assert and ?_assert
291836e
Replace anonymous test functions w/real ones.
cf05b93
Test output changed as advised.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,9 @@ | |
| couch, | ||
| ets_lru, | ||
| fabric, | ||
| cassim | ||
| cassim, | ||
| mem3, | ||
| global_changes | ||
| ]}, | ||
| {mod, {chttpd_app,[]}} | ||
| ]}. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| %% Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| %% use this file except in compliance with the License. You may obtain a copy of | ||
| %% the License at | ||
| %% | ||
| %% http://www.apache.org/licenses/LICENSE-2.0 | ||
| %% | ||
| %% Unless required by applicable law or agreed to in writing, software | ||
| %% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| %% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| %% License for the specific language governing permissions and limitations under | ||
| %% the License. | ||
|
|
||
| %% @doc Configurable, dynamic creation of endpoint handler callback indirections. | ||
|
|
||
| -module(chttpd_handler). | ||
|
|
||
| -export([build/0, build/1, url_handler/1, db_url_handlers/0, | ||
| design_url_handlers/0]). | ||
|
|
||
| -vsn(4). | ||
|
|
||
| %% @doc a complete configuration data set | ||
| -type config() :: [Function::{Name::atom(), clauses|list, [bind()]}]. | ||
|
|
||
| %% @doc one essential pair of a pattern and the fun to be returned for it | ||
| -type bind() :: {Endpoint::term(), MFA::{atom(), atom(), integer()}}. | ||
|
|
||
| -spec url_handler(Endpoint::list()) -> Handler::fun(). | ||
| %% @doc Dispatch endpoint to fun, wrapper to hide dynamic module. | ||
| url_handler(Endpoint) -> | ||
| chttpd_dyn_handler:url_handler(Endpoint). | ||
|
|
||
| -spec db_url_handlers() -> [{Endpoint::list(), Handler::fun()}]. | ||
| %% @doc Get a list of endpoints and handler funs, wrapper to hide dyn module. | ||
| db_url_handlers() -> | ||
| chttpd_dyn_handler:db_url_handlers(). | ||
|
|
||
| -spec design_url_handlers() -> [{Endpoint::list(), Handler::fun()}]. | ||
| %% @doc Get a list of endpoints and handler funs, wrapper to hide dyn module. | ||
| design_url_handlers() -> | ||
| chttpd_dyn_handler:design_url_handlers(). | ||
|
|
||
| -spec build() -> ok | []. | ||
| %% @doc Create the dynamic handler functions from ini file. | ||
| build() -> | ||
| build(load_defs()). | ||
|
|
||
| -spec build(HandlerCfg::config()) -> ok. | ||
| %% @doc Compile the complete syntax tree, purge and load the dynamic module | ||
| build(Cfg) when is_list(Cfg) -> | ||
| Opts = [verbose, report_errors], | ||
| {ok, Mod, Bin} = compile:forms(forms(chttpd_dyn_handler, Cfg), Opts), | ||
| % don't code:purge(Mod), | ||
| {module, Mod} = code:load_binary(Mod, atom_to_list(Mod) ++ ".erl", Bin), | ||
| ok. | ||
|
|
||
| -spec load_defs() -> CombinedHandlerCfg::config(). | ||
| %% @doc assemble the configuration from the chttpd_handler.cfg of all apps. | ||
| load_defs() -> | ||
| {AllURLHandlers, AllDBHandlers, AllDesignHandlers} = lists:foldl( | ||
| fun(App, {URLHandlers, DBHandlers, DesignHandlers}) -> | ||
| Defs = load_defs(App), | ||
| {URLHandlers ++ [ B || {docs, clauses, B} <- Defs ], | ||
| DBHandlers ++ [ B || {db, list, B} <- Defs ], | ||
| DesignHandlers ++ [ B || {design, list, B} <- Defs ]} | ||
| end, | ||
| {[],[],[]}, | ||
| [element(1, A) || A <- application:loaded_applications()]), | ||
| [{url_handler, clauses, lists:flatten(AllURLHandlers)}, | ||
| {db_url_handlers, list, lists:flatten(AllDBHandlers)}, | ||
| {design_url_handlers, list, lists:flatten(AllDesignHandlers)}]. | ||
|
|
||
| -spec load_defs(AppName::atom()) -> OneAppsHandlerCfg::config(). | ||
| %% @doc assemble the configuration from the chttpd_handler.cfg of all apps. | ||
| load_defs(App) -> | ||
| case code:priv_dir(App) of | ||
| {error, _Error} -> | ||
| []; | ||
| Dir -> | ||
| Path = Dir ++ "/chttpd_handler.cfg", | ||
| case file:consult(Path) of | ||
| {ok, Defs} -> | ||
| [ check_def(Def, Path) || Def <- Defs ]; | ||
| {error, _Error} -> | ||
| [] | ||
| end | ||
| end. | ||
|
|
||
| check_def({_, _, []}=Def, Path) -> | ||
| throw({no_defs_error, Def, Path}); | ||
| check_def({docs, clauses, B}, Path) -> | ||
| {docs, clauses, sort(check_dupes(check_bindings(B, list, 1, Path), Path))}; | ||
| check_def({db, list, B}, Path) -> | ||
| {db, list, check_dupes(check_bindings(B, binary, 2, Path), Path)}; | ||
| check_def({design, list, B}, Path) -> | ||
| {design, list, check_dupes(check_bindings(B, binary, 3, Path), Path)}; | ||
| check_def(Def, Path) -> | ||
| throw({tag_error, Def, Path}). | ||
|
|
||
| check_bindings([{Endpoint, {M, F, Arity}}=Good | More], list, Arity, Path) | ||
| when is_list(Endpoint), is_atom(M), is_atom(F), is_integer(Arity) -> | ||
| [Good | check_bindings(More, list, Arity, Path)]; | ||
| check_bindings([{Endpoint, {M, F, Arity}}=Good | More], binary, Arity, Path) | ||
| when is_binary(Endpoint), is_atom(M), is_atom(F), is_integer(Arity) -> | ||
| [Good | check_bindings(More, binary, Arity, Path)]; | ||
| check_bindings([{'_', {M, F, Arity}}=Good | More], Type, Arity, Path) | ||
| when is_atom(M), is_atom(F), is_integer(Arity) -> | ||
| [Good | check_bindings(More, Type, Arity, Path)]; | ||
| check_bindings([Bad | _], _, Arity, Path) -> | ||
| throw({syntax_or_arity_error, Bad, exptected_arity, Arity, Path}); | ||
| check_bindings([], _, _, _) -> | ||
| []. | ||
|
|
||
| -spec sort(Cfg::config()) -> config(). | ||
| %% @doc make sure that any _ is the last clause of a generated function | ||
| sort(Cfg) -> | ||
| lists:sort( | ||
| fun ({'_',_}, _) -> false; | ||
| (_, {'_',_}) -> true; | ||
| (_, _) -> true | ||
| end, | ||
| Cfg). | ||
|
|
||
| -spec check_dupes(Cfg::config(), Path::list()) -> config() | []. | ||
| %% @doc crash if an endpoint is defined twice | ||
| check_dupes(Cfg, Path) -> | ||
| lists:sort( | ||
| fun ({E,_}=Def1, {E, _}=Def2) -> | ||
| throw({duplicate_endpoint, E, Def1, Def2, Path}); | ||
| (_, _) -> true | ||
| end, | ||
| Cfg). | ||
|
|
||
| -spec forms(Mod::atom(), Defs::[bind()]) -> erl_syntax:syntaxTree(). | ||
| %% @doc The complete syntax tree of the dynamic module | ||
| forms(Mod, Defs) -> | ||
| Statements = [ | ||
| module_stmt(Mod), | ||
| export_stmt( | ||
| [{Name, case Lay of clauses -> 1; list -> 0 end} | ||
| || {Name, Lay, _} <- Defs ]) | ||
| | [ binding_function(Name, Lay, Def) || {Name, Lay, Def} <- Defs ]], | ||
| [ erl_syntax:revert(X) || X <- Statements]. | ||
|
|
||
| -spec module_stmt(ModuleName::atom()) -> erl_syntax:syntaxTree(). | ||
| %% @doc Create syntax tree for the module statement of the dynamic module | ||
| module_stmt(Name) -> | ||
| erl_syntax:attribute( | ||
| erl_syntax:atom(module), | ||
| [erl_syntax:atom(Name)]). | ||
|
|
||
| -spec export_stmt([Exports::{Name::atom(), Arity::integer()}]) -> | ||
| erl_syntax:syntaxTree(). | ||
| %% @doc Create syntax tree for the export statement of the dynamic module | ||
| export_stmt(Exports) -> | ||
| erl_syntax:attribute( | ||
| erl_syntax:atom(export), | ||
| [erl_syntax:list( | ||
| [ erl_syntax:arity_qualifier( | ||
| erl_syntax:atom(Name), | ||
| erl_syntax:integer(Arity)) | ||
| || {Name, Arity} <- Exports ] | ||
| )]). | ||
|
|
||
| -spec binding_function(Name::atom(), clauses | list, [bind()]) -> | ||
| erl_syntax:syntaxTree(). | ||
| %% @doc Create syntax subtree for a function that either has multiple clauses | ||
| %% or returns a list of tuples of a tag and a fun. | ||
| binding_function(Name, list, Defs) -> | ||
| erl_syntax:function( | ||
| erl_syntax:atom(Name), | ||
| [erl_syntax:clause([], none, | ||
| [erl_syntax:list( | ||
| [erl_syntax:tuple([ | ||
| erl_syntax:abstract(P), | ||
| create_fun(Def)]) | ||
| || {P, Def} <- Defs ])])]); | ||
| binding_function(Name, clauses, Defs) -> | ||
| erl_syntax:function( | ||
| erl_syntax:atom(Name), | ||
| [create_fun_clause(Def) || Def <- sort(Defs)]). | ||
|
|
||
| -spec create_fun_clause(bind()) -> erl_syntax:syntaxTree(). | ||
| %% @doc Create syntax subtree for a function clause with one implicit fun call. | ||
| create_fun_clause({P, MFA}) -> | ||
| erl_syntax:clause( | ||
| [case P of | ||
| '_' -> erl_syntax:underscore(); | ||
| _ -> erl_syntax:abstract(P) | ||
| end], | ||
| none, | ||
| [create_fun(MFA)]). | ||
|
|
||
| -spec create_fun(MFA::{Module::atom(), Function::atom(), Arity::integer()}) -> | ||
| erl_syntax:syntaxTree(). | ||
| %% @doc Create syntax subtree for an implicit fun call. | ||
| create_fun({M, F, A}) -> | ||
| erl_syntax:implicit_fun( | ||
| erl_syntax:atom(M), | ||
| erl_syntax:atom(F), | ||
| erl_syntax:integer(A)). | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Seems I miss that moment. Why not to reuse
configand have all the handlers defined there? We already keep httpd ones there and this gives us dynamic control over them with no server restart.