Sunday, August 7, 2011

Nitrogen, an Erlang web application library/framework Part 9


For those just now encountering this series, you'll want to start with Part 1, and at least have Introduction to Nitrogen open in another tab.


Slide 78: Extending Nitrogen: Handlers - Part 1


Ok, good, you can override nearly anything you might care about.


Slide 79: Extending Nitrogen: Handlers - Part 2


Do not attempt to override something in a stage you haven't reached yet. Pages go through ten stages - but how do you specify where in the process your code executes?


Slide 80: Extending Nitrogen: Handlers - Part 3


Answer: you specify a behavior corresponding to the stage of processing you're affecting, and write functions appropriate to that behavior. It's not clear where the new code is supposed to live. Is there other code that appears similar to my_security handler?

bash-4.1$ pwd
/home/sps/erlang-libs/nitrogen/rel/nitrogen/site
bash-4.1$ find . -name '*security*' -print
bash-4.1$ cd ..
bash-4.1$ find . -name '*security*' -print
bash-4.1$ cd ..
bash-4.1$ find . -name '*security*' -print
bash-4.1$ cd ..
bash-4.1$ find . -name '*security*' -print
./Quickstart/src/demos/demos_security_restricted.erl
./Quickstart/src/demos/demos_security_login.erl
./Quickstart/src/demos/demos_security.erl
./apps/nitrogen/src/handlers/security
./apps/nitrogen/src/handlers/security/http_basic_auth_security_handler.erl
./apps/nitrogen/src/handlers/security/security_handler.erl
./apps/nitrogen/src/handlers/security/default_security_handler.erl
./apps/nitrogen/ebin/default_security_handler.beam
./apps/nitrogen/ebin/security_handler.beam
./apps/nitrogen/ebin/http_basic_auth_security_handler.beam
bash-4.1$ pwd
/home/sps/erlang-libs/nitrogen

Hmm. I don't want to plunk it down with the other canonical source. When we added a new action, it went in site/src/actions. Let's put our handler in site/src/handlers.


Slide 80: Extending Nitrogen: Handlers - Part 3 (more)


Um, nitrogen_inets.erl. That's located...
bash-4.1$ pwd
/home/sps/erlang-libs/nitrogen/rel/nitrogen/site
bash-4.1$ find . -name nitrogen_inets.erl -print
bash-4.1$ cd ..; find . -name nitrogen_inets.erl -print
bash-4.1$ cd ..; find . -name nitrogen_inets.erl -print
./overlay_inets/site/src/nitrogen_inets.erl
bash-4.1$

Ok, I think that was created when we created the site. I'm going to guess that a new file in our src directory will get combined with the existing code in some useful way, so I'm going to put this in /home/sps/erlang-libs/nitrogen/rel/nitrogen/site. Looking at the other nitrogen_inets.erl file, we see that there are (of course) -module and -export statements in the file as well. Also, looking at that file, it appears we're replacing its function instead of augmenting it.

Let's fire up nitrogen again:



bash-4.1$ cd bin
/home/sps/erlang-libs/nitrogen/rel/nitrogen/bin
bash-4.1$ ls
dev nitrogen
bash-4.1$ ./nitrogen console
Exec: /home/sps/erlang-libs/nitrogen/rel/nitrogen/erts-5.8.2/bin/erlexec -boot /home/sps/erlang-libs/nitrogen/rel/nitrogen/releases/2.0.4/nitrogen -embedded -config /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/app.config -config /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/mochiweb.config -args_file /home/sps/erlang-libs/nitrogen/rel/nitrogen/etc/vm.args -- console
Root: /home/sps/erlang-libs/nitrogen/rel/nitrogen
Erlang R14B01 (erts-5.8.2) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:5] [kernel-poll:true]

Eshell V5.8.2 (abort with ^G)
(nitrogen@127.0.0.1)1> Starting Mochiweb Server (nitrogen) on 0.0.0.0:8000, root: './site/static'

...and in another window

bash-4.1$ ./dev compile
:: MAKE - site
Recompile: ./src/nitrogen_inets
Recompile: ./src/handlers/my_security_handler
:: MAKE - ./site
:: Done!
bash-4.1$

And now, did all that work? Pointing the web browser at http://localhost:8000, we get prompted for a login.

Not behaving as expected: I can still get to http://localhost:8000/chat, which, not being prefixed by "my_", shouldn't work.


WHOOPS!


At some point I must have played with using the mochiweb server instead of inets, and that means nitrogen_inets.erl is getting ignored. Let's check the git log to see if I can back up to where I switched.


Way, way back at the 4th screen! Which I didn't even think was worth mentioning. Sheesh. Ok, so now I've bought myself some, uh, experience, and I'll need to come up with nitrogen_mochiweb.erl instead of nitrogen_inets.erl. Let's see if I can pull that off. How about doing this:



bash-4.1$ git diff
diff --git a/rel/nitrogen/site/src/nitrogen_mochiweb.erl b/rel/nitrogen/site/src
index 0ee336a..f2c7dd0 100644
--- a/rel/nitrogen/site/src/nitrogen_mochiweb.erl
+++ b/rel/nitrogen/site/src/nitrogen_mochiweb.erl
@@ -25,4 +25,5 @@ loop(Req) ->
RequestBridge = simple_bridge:make_request(mochiweb_request_bridge, {Req, D
ResponseBridge = simple_bridge:make_response(mochiweb_response_bridge, {Req
nitrogen:init_request(RequestBridge, ResponseBridge),
+ nitrogen:handler(my_security_handler, []),
nitrogen:run().

Stop the nitrogen server, compile that, then start nitrogen again. OK, the index now gives a blank page, probably because it isn't prefixed with "my_". But http://localhost:8000/my/page doesn't work either. I'm going to need a deeper dive into the code to make this work.

No comments:

Post a Comment