Github user kxepal commented on a diff in the pull request:
https://github.com/apache/couchdb-chttpd/pull/10#discussion_r23889246
--- Diff: src/chttpd_handler.erl ---
@@ -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",
--- End diff --
Seems I miss that moment. Why not to reuse `config` and have all the
handlers defined there? We already keep httpd ones there and this gives us
dynamic control over them with no server restart.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---