Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Webmachine

Jade Allen
October 16, 2013

Introduction to Webmachine

Webmachine is a toolkit to build REST APIs implemented in Erlang.

Jade Allen

October 16, 2013
Tweet

More Decks by Jade Allen

Other Decks in Technology

Transcript

  1. -module(hello_world_resource). -export([init/1, to_html/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, "Hello, world"}. to_html(Req,

    State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}.
  2. -module(hello_world_resource). -export([init/1, to_html/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, "Hello, world"}. to_html(Req,

    State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}.
  3. -module(hello_world_resource). -export([init/1, to_html/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, "Hello, world"}. to_html(Req,

    State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}.
  4. -module(hello_world_resource). -export([init/1, to_html/2, generate_etag/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, "Hello, world"}.

    to_html(Req, State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}. generate_etag(Req, State) -> {mochihex:to_hex(crypto:md5(State)), Req, State}.
  5. -module(hello_world_resource). -export([init/1, to_html/2, generate_etag/2]). -export([encodings_provided/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, "Hello,

    world"}. to_html(Req, State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}. generate_etag(Req, State) -> {mochihex:to_hex(crypto:md5(State)), Req, State}. encodings_provided(Req, State) -> {[{"gzip", fun(X) -> zlib:gzip(X) end}], Req, State}.
  6. -module(hello_world_resource). -export([init/1, to_html/2, to_json/2, generate_etag/2]). -export([encodings_provided/2, content_types_provided/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) ->

    {ok, "Hello, world"}. content_types_provided(Req, State) -> {[{"text/html", to_html}, {"application/json", to_json}], Req, State}. to_json(Req, State) -> {mochijson2:encode({struct, [{greeting, list_to_binary(State)}]}), Req, State}. to_html(Req, State) -> {io_lib:format("<html><head><title>~s</title></ head><body>~s</body></html>", [State, State]), Req, State}. %% ...
  7. % bind 2nd element to 'foo' {["a", foo], some_resource, []}

    % retrieve with % wrq:path_info(foo, Req) % URL of /a/b returns "b"