Thursday, March 22, 2012

Cowboy + ErlyDTL

This is a continuation of part 1 so I'm going to assume we're starting where we left off there.

Now that we have a Cowboy server that can handle static pages, we need to set it up to handle dynamic pages.  In comes ErlyDTL.  This is basically going to be the same as OJ's tutorial but for Cowboy instead of webmachine.

ErlyDTL Install
Change your rebar.config to include the ErlyDTL dependency.
Run ./rebar get-deps compile to make sure that step worked.

Make a page controlled by code
Modify src/simple_server_http.erl to add a path and handler name to the dynamic page you're creating in dispatch_rules/0.  In this case, requests for the 'pony' resource (http://127.0.0.1/pony) should be handled by simple_server_http_pony.
Create src/simple_server_http_pony.erl
Create templates/mylittlepony.dtl

Remove the previous release, compile, generate, run.
Go to http://127.0.0.1/pony.  Dazzleglow is everyone's favorite pony.

Take in user input
If only the world were so simple that we could just create answers...  The following shows how to deal with GET/POST parameters so we can take input from those pesky users.

Create a new dispatch rule and handler for a page called captainflint.  I'm going to assume you know how to add a dispatch rule to src/simpleserver_http.erl at this point.


Go to http://127.0.0.1/captainflint?get_input=Arrr.  Thar she blows.  Now you have a working example of how to access GET and multi-valued POST parameters.

No comments:

Post a Comment