Erlang Webmachine REST -- How-to and notes

2   Generating a Webmachine starter application

Instructions can be found here under "Quick Start".

These instructions use rebar3. For more on rebar3, see:

Step 1. Install rebar3, if you do not already have it.

Step 2. Create the Webmachine template for rebar3:

$ mkdir -p ~/.config/rebar3/templates
$ git clone git:// ~/.config/rebar3/templates

Step 3. Create a new Webmachine starter application:

$ cd my_work_directory
$ rebar3 new webmachine myapp

Step 4. Build (compile) your application:

$ cd myapp
$ rebar3 release

Now, you can edit (possibly following the notes below), re-build, and test your application, repeatedly.

3   Configuration

You can specify configuration options in myapp/config/sys.config, then retrieve them and return them from myapp/src/myapp_config:web_config/0.

Here is an example of myapp/config/sys.config:

  {'myapp01', [{web_ip, ""},
                {web_port, 8080}]}

And, here is an example of myapp/src/myapp_config:web_config/0:

web_config() ->
    {ok, App} = application:get_application(?MODULE),
    {ok, Ip} = application:get_env(App, web_ip),
    {ok, Port} = application:get_env(App, web_port),
        {ip, Ip},
        {port, Port},
        {log_dir, "priv/log"},
        {dispatch, dispatch()}

4   Dispatching and resources

In Webmachine, a resource is implemented in an Erlang module in myapp/src, for example, myapp/src/myapp_simple_resource.erl. This module implements a number of functions, some required and some optional. For a description of those functions, see:

Dispatching guides Webmachine to that module based on a URI, and that "routes" that you specify in myapp/src/myapp_config:dispatch/0.

The list returned by dispatch/0 specifies a set of routes. Each "route" specifies (1) a pattern to be matched against the request URI, (2) a resource implementation module, and (3) configuration arguments to be passed to the init/1 function in the resource implementation module.

Keep in mind that you can retrieve query strings using wrq:get_qs_value/2.

And, you can retrieve parts of the URI that match atoms using wqr:get_path_info/2 and the atom that was replaced.

For more on developing REST-style applications in Erlang and Webmachine, see: