Erlang WebDev with WebMachine

Tim McGilchrist (@lambda_foo)

Outline

Web Development

Any language worth it's salt needs to provide a web development library or three.
www.coboloncogs.org anyone? Srsly

General approaches to the web:

What is Webmachine?

"Written in Erlang, Webmachine is an application layer that adds server-side HTTP semantic awareness on top of the excellent bit-pushing and HTTP syntax-management provided by mochiweb, " wiki.basho.com/Webmachine.html

WebMachine is not a "framework"!

HTTP is Simple, Right?

Hello World WebMachine style

-module(hello_resource).
-export([init/1, to_html/2]).
-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
    {ok, undefined}.

to_html(ReqData, State) ->
    {"Hello, Webmachine world", ReqData, State}.

  

WebMachine Layout

Dispatch Rules

Dispatch Example

    {["todos", "*"], sm_todos_resource, []}.

    GET "/todos/12/author"

    "12/author"        => wrq:disp_path
    "/todos/12/author" => wrq:path
    []                 => wrq:path_info
    ["12", "author"]   => wrq:path_tokens
  

Resources

WebMachine is a resource server for the web!

Driven via HTTP methods; GET, PUT, POST, DELETE

Uses correct HTTP status codes.

Well defined place to handle each HTTP method.

Example

A Todo Resource pulled from a demo app.

Example Cont

Problems with POST

ErlDTL Templates

Django style templates for Erlang, similar to JSP and ERB templates.
https://github.com/evanmiller/erlydtl

Building with Rebar

  1. Create a top level rebar.conf
  2. Put in your dependencies
  3. Compile and off you go

Recap

You need to provide all the plumbing.
Want to serve JS & CSS, you write the resource.
Authorisation? Callback provided and you write the rest.
DB / ORM? Same.

Get the picture?

Resources

Questions?

One More thing!

Feedback and suggestions for Erlang Factory Lite.

/

#