Pages

Saturday, July 6, 2013

Erlang Web Application: Getting Started

Lately I've been getting back into building my own web application. It started with some MongoDB which led to Python and Bottle through this video to Apache to Yaws and Erlang.  I've finally settled on working through some of the examples included in the O'Reily book Building Web Applications with Erlang.

It's been a lot of installation and figuring out what goes where, but I'm starting to see some cool stuff. I really like the idea of running a web application through Yaws. Erlang has always made a lot of sense to me with it's light-weight application threads and message passing.

More Details

I didn't do anything special from the standard way of installing packages in Ubuntu. I don't think there's anything special other than
steve@ubuntu$ sudo apt-get install yaws
This should install all the dependencies, including Erlang which you'd need to get started. The yaws service is started, using 
/usr/share/yaws
  as a home directory for the server. Starting/stopping/restarting the service is as easy as:

steve@ubuntu$ sudo service yaws <command>

 First Examples

  I just have a couple very simple examples right now. Hopefully more to come shortly. One thing I've found is that if you modify index.html in the server's home directory, changes can be seen by visiting localhost:8080/index.html but not simply localhost:8080 until you perform a server restart (or maybe reload). Most likely due to file handles of the server. Fortunately, creating examples and running <erl> tags within new files works because you specify the URL for the files explicitly, so you don't have to restart the server between every step.

First output

The first example was straight from the book. Creating method.yaws in the server home directory:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
Then  browsing to localhost:8080/method.yaws shows that you used a get request.

 Simple Redirect

The next simple example is to redirect to another page:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
This simply redirects to the method.yaws example above.
 

No comments:

Post a Comment