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.

3 comments: