nitrogen / simple_bridge

A simple, standardized interface library to Erlang HTTP Servers.
MIT License
112 stars 76 forks source link

Allow cowboy_dispatch_fun to access to the default Nitrogen routing settings #71

Closed Olivier-Boudeville closed 5 years ago

Olivier-Boudeville commented 5 years ago

Allow any third-party cowboy_dispatch_fun to access to the default Nitrogen routing by calling get_dispatch/2.

Olivier-Boudeville commented 5 years ago

Build is OK, test suite mostly fails, but I do not think it is related to this merge request, we have plenty of messages like: ===Error in Multipart: {file_too_big,"data2"} [...] (and it would be OK with Erlang 20.3 but not with other versions) A problem with the continuous integration system?

Olivier-Boudeville commented 5 years ago

Just committed an improved version of the pull request.

The principle is that third-party code can now obtain the Nitrogen-specific dispatch information in order to devise/add any extra dispatches of its own (ex: a Foobar server having multiple vhosts to serve in addition to a Nitrogen website) .

For example, in simple_bridge.config, one may specify: { cowboy_dispatch_fun, { foobar_for_cowboy, get_foobar_host_dispatch } }

Then, in foobar_for_cowboy.erl, one can define: get_foobar_host_dispatch() -> % As in cowboy_simple_bridge_sup:build_dispatch/0: { DocRoot, StaticPaths } = simple_bridge_util:get_docroot_and_static_paths( cowboy ), % Gets Nitrogen default settings: { StaticDispatches, HandlerModule, HandlerOpts } = cowboy_simple_bridge_sup:get_dispatch_info( DocRoot, StaticPaths ), AllDispatches = get_foobar_host_dispatch( StaticDispatches, HandlerModule, HandlerOpts ), cowboy_router:compile( AllDispatches ).

where get_foobar_host_dispatch/3 may define, in addition to a dispatch for the Nitrogen website itself, a bunch of dispatches corresponding to static virtual hosts of interest.

Olivier-Boudeville commented 5 years ago

Hum, unless adding a {'_', cowboy_simple_bridge_anchor,[]} dispatch rule could be a way of doing the same, as hinted by the comment in simple_bridge.config? In this case, HandlerModule and HandlerOpts would be the same as with default settings, but can we trust StaticPaths with said rule to be the same as the default one, obtained from simple_bridge_util:get_docroot_and_static_paths(cowboy)?

choptastic commented 5 years ago

This is great! Thanks! I like the idea of being able to use the default-generated dispatch table as a basis for another table. One can always override the default anchor module if the user needs to do something more interesting than the default behavior, but I like this. I will merge it momentarily. Don't worry about the broken builds. The test suite throws a pile of expected errors, but sometimes the webmachine tests fail on large attachments - don't know why.