Showing posts with label cowboy. Show all posts
Showing posts with label cowboy. Show all posts

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.

Monday, March 19, 2012

A very basic Cowboy setup

I'm new to using Erang in a "real" way, but I've been wanting to use it for a while.  Looking around at the Erlang landscape these days, I don't feel bad for having waited four+ years.  Some projects have come onto the scene in the last couple of years that make development in Erlang a practical reality for those of us who don't have the time to develop everything from first principles.  Basho's webmachine was very close to being my web server of choice, but I needed web sockets so Cowboy seemed like a better choice than cobbling together my own webmachine/web socket router.

For today's deposit into the Google karma bucket, I'm going to run through the setup for a super duper simple Cowboy web server so you can move past the plumbing stage faster.  I started from the BigWig project and stripped stuff away until I had a web server that would serve static pages and 404s.

Pre-requisites (Blogger, why do I have to bold this instead of just selecing an <H2>?)
Erlang (R15B), rebar, Unix-y OS (Mac OSX in my case)

Process
Setup
Set up directories and copy over rebar binary

Create rebar.config in your project directory

Run rebar commands to create a basic server and release.  It's easier to name everything the same as the project directory than it is to figure out what to change in the configs.

Modify rel/reltool.config so that lib_dirs points to the directory that contains the src directory - ["../.."]

Make a simple web server
Add 5 new files and modify one of the generated files.  I think there's a way to have rebar generate all of this, but I'm not going to bother figuring that out until I've got a better sense of what all I want to have generated for me on future projects.

Create src/simple_server.erl

Create src/simple_server_http.erl

Create src/simple_server_http_catchall.erl

Create src/simple_server_http_static.erl

Create priv/html/index.html

Modify the init/1 function in src/simple_server_sup.erl to include your HTTP handler

Build and test
The following will pull down Cowboy, compile all your files, and generate a release that you can run:
./rebar get-deps compile generate

From the rebar tutorials I've seen, you should be able to call:
./rel/simple_server/bin/simpler_server start

This call returns without error and if you look at your processes it looks like something's going on, but there'll be nothing for you in your web browser.

Instead, call the following the start up your server:
sudo erl -sname simple_server -pa $PWD/ebin $PWD/deps/*/ebin -boot start_sasl -s simple_server

Make sure to call it with sudo since we have the port set to 80.  If you set it to something like 8000 you don't have to call it with sudo.

Open up http://127.0.0.1 in your browser and celebrate.  Open up http://127.0.0.1/blah in your browser and celebrate some more.

More
Yay, now we have a lame server.  Next up, we need to add templating and a datastore to do more interesting stuff.  See part 2.