Nitrogen and Chicago Boss

41 views
Skip to first unread message

rlander

unread,
Feb 25, 2016, 6:13:55 PM2/25/16
to Nitrogen Project / The Nitrogen Web Framework for Erlang
Hey guys,

I have a Chicago Boss app that is just a REST backend (produces JSON). I was wondering if it is possible to integrate Nitrogen into this app for the frontend.

To be clear, I'm not trying to use Nitrogen within CB; I just want to use the same application to route, for example, "/api" to CB and "/admin" to Nitrogen, so that I don't have to deploy two separate releases. Is this possible? I've read this link [0] but I'm probably missing something because, although both frameworks share the same application, I could not figure out how to hook the routes together.

Thanks!

Jesse Gumm

unread,
Feb 25, 2016, 7:40:43 PM2/25/16
to nitrogenweb
Hi Rodrigo,

This is an interesting idea, and not one pursued too deeply in the past.

I believe it can be doable, but it may be a little finnicky and will
certainly require some trial and error:

My gut is telling me that the best approach is this:

1) Pick a backend (cowboy, yaws, whatever)

2) Deploy a custom simple_bridge anchor module based on the backend's
anchor module (example:
https://github.com/nitrogen/simple_bridge/blob/master/src/cowboy_bridge_modules/cowboy_simple_bridge_anchor.erl),
which would then choose the "handler" module with the desired one
based on the URL as retrieved from the Req objects, rather than
choosing the handler from the application env vars (as it does here:
https://github.com/nitrogen/simple_bridge/blob/master/src/cowboy_bridge_modules/cowboy_simple_bridge_anchor.erl#L30-L49)

CB's simple_bridge handler is 'boss_simple_bridge_handler'
Nitrogen's simple_bridge handler is the module 'nitrogen'

You could probably make a function like:

routed_handler_mod(Req) ->
case cowboy_req:path(Req) of
{"api/" ++ _, _} -> boss_simple_bridge_handler;
{"admin/" ++ _, _} -> nitrogen
end.

Then replace the calls in the above linked anchor module to:

application:get_env or simple_bridge_utils:get_env to routed_handler_mod(Req)

3) The last thing you'll probably need to do is to make sure to route
the requests to the new anchor module:

So while simple_bridge's cowboy supervisor creates a dispatch table
that looks like this:

[{'_',[],
[{[<<"js">>,'...'],
[],cowboy_static,
{dir,"./site/static/js/",[{mimetypes,cow_mimetypes,all}]}},
{[<<"css">>,'...'],
[],cowboy_static,
{dir,"./site/static/css/",[{mimetypes,cow_mimetypes,all}]}},
{[<<"images">>,'...'],
[],cowboy_static,
{dir,"./site/static/images/",[{mimetypes,cow_mimetypes,all}]}},
{[<<"nitrogen">>,'...'],
[],cowboy_static,
{dir,"./site/static/nitrogen/",[{mimetypes,cow_mimetypes,all}]}},
{[<<"favicon.ico">>],
[],cowboy_static,
{file,"./site/static/favicon.ico",[{mimetypes,cow_mimetypes,all}]}},
{'_',[],cowboy_simple_bridge_anchor,[]}]}]

You could modify just the last line of that to be:

{'_',[],my_custom_cowboy_simple_bridge_anchor,[]}]}]


It's a fair bit of work in a number of different areas, but the
alternative is just running a custom fork of simple_bridge that makes
those changes directly (which also isn't a terrible idea - Indeed that
gives me the idea of maybe allowing the "handler" module config to be
a function call).

As you plunk through this, be sure to keep us posted how you do or
what you get stuck on, because I find this to be an interesting idea,
and I'm very curious how it'll play out.

The alternative to all this, of course, is just spinning up two VMs
(one for CB and one for Nitrogen), and using nginx to route to each.
But as you said, you would like to avoid that.

-Jesse
> --
> You received this message because you are subscribed to the Google Groups
> "Nitrogen Project / The Nitrogen Web Framework for Erlang" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nitrogenweb...@googlegroups.com.
> To post to this group, send email to nitro...@googlegroups.com.
> Visit this group at https://groups.google.com/group/nitrogenweb.
> For more options, visit https://groups.google.com/d/optout.



--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

rlander

unread,
Feb 26, 2016, 1:29:01 AM2/26/16
to Nitrogen Project / The Nitrogen Web Framework for Erlang
Thanks Jesse, I'll give it a try. I did think about simply forking SimpleBridge, but I already have an obsolete fork of CB and boss_db so I'll try your approach and report back on my findings. Again, thanks for explanation!

Jesse Gumm

unread,
Feb 26, 2016, 2:55:49 PM2/26/16
to nitrogenweb
Oh, I completely forgot to include that it will also help to use
nitrogen's `embed` script which can help kickstart an existing OTP app
with Nitrogen.

You can run it from your CB installation, then follow the onscreen
prompts. It's found in the root of the nitrogen repo:

https://github.com/nitrogen/nitrogen/blob/master/embed

-Jesse
Reply all
Reply to author
Forward
0 new messages