A RESTful Interface for Erlang Code using Webmachine

24
A RESTful interface for Erlang code using Webmachine Copyright 2014 Carl Wright

Transcript of A RESTful Interface for Erlang Code using Webmachine

Page 1: A RESTful Interface for Erlang Code using Webmachine

A RESTful interface for Erlang code

using Webmachine

Copyright 2014 Carl Wright

Page 2: A RESTful Interface for Erlang Code using Webmachine

RESTful interface

• Exposes your app as resources on the Web.

• Resources respond to 6 HTTP methods: GET, HEAD, POST, PUT, DELETE and OPTIONS

Page 3: A RESTful Interface for Erlang Code using Webmachine

HTTP: interface to your erlang app

• Start with an erlang app

• add webmachine code

• Write the dispatch table & web app start

• Write the resource code indicated by the dispatch table

Erlang app

WebmachineHTTP:

Page 4: A RESTful Interface for Erlang Code using Webmachine

A specific application

Webui holds the code to respondto URIs

Webmachine provides anHTTP server to handleURI requests on a specificchannel.

Page 5: A RESTful Interface for Erlang Code using Webmachine

Adding webmachine

• Go to https://github.com/basho/webmachine/wiki

• To see the documentation and to fork a copy of webmachine

• Add it to your erlang application.

Page 6: A RESTful Interface for Erlang Code using Webmachine

The others apps

• To run webmachine, you need the following apps:– mochiweb

• http interaction support library

– crypto• Cryptographic functions to support secure web

– inets• Base webservices (ftp, tftp, http, https, etc.)

Page 7: A RESTful Interface for Erlang Code using Webmachine

Adding to your app definition

• You have a “.rel” to define your application release.

Page 8: A RESTful Interface for Erlang Code using Webmachine

Yes, you need mochiweb, too.

Mochiweb is installed alongwebmachine and your “webui”folder.

Crypto and inets are in the standard erlang libraries.

You just include them inYour application definition.

Page 9: A RESTful Interface for Erlang Code using Webmachine

Release configuration

• Add “webmachine” to your release configuration files for “relx” or “rebar”.

• These are the most common release managers for erlang systems.

Page 10: A RESTful Interface for Erlang Code using Webmachine

Inside your web UI

• OTP folder structure

• ebin holds .app file and compiled binaries

• priv holds the dispatch table.

• src holds the application source code.

Page 11: A RESTful Interface for Erlang Code using Webmachine

Webui.app (in ebin folder)

Page 12: A RESTful Interface for Erlang Code using Webmachine

The src folder

Page 13: A RESTful Interface for Erlang Code using Webmachine

Who does what?

webui_app

webui_sup

webmachinewebui_resource.erl

webui_resource_status.erldispatch

table

Page 14: A RESTful Interface for Erlang Code using Webmachine

A simple dispatch table

1. http://host/device/status/73482720002. http://host {anything else}

Page 15: A RESTful Interface for Erlang Code using Webmachine

Dispatch pattern

{ <pattern>, <resource-mod>, <init-params>}

i.e.

{[“a”], module_a, []}.

matches http://host/a & calls module_a

{[“a”, varname], module_a, []}.

match http://host/a/Carl & calls module_a with “varname” = “Carl”

Page 16: A RESTful Interface for Erlang Code using Webmachine

Left side pattern

• Is a list of any atom, string or star

• Breaks the URL on slashes

• Strings must match the URL contents

• Atoms associate with the value so that you retrieve their value

• Star is “*” and must be the last in the list when present.

Page 17: A RESTful Interface for Erlang Code using Webmachine

A CRUD dispatch

• % Accounts• {["account","add"], webui_resource_account,

[add, [{'POST', <<"accounts">>}]]},• {["account","delete",id], webui_resource_account,

[delete, [{'POST', <<"accounts">>}]]},• {["account","edit",id], webui_resource_account,

[edit, [{'POST', <<"accounts">>}]]},• {["accounts"], webui_resource_account,

[account_many,[{'GET', <<"accounts">>}]]},• {["json","accounts"], webui_resource_account,

[accounts, [{'GET', <<"accounts">>}]]},

Page 18: A RESTful Interface for Erlang Code using Webmachine

A real resource function

Page 19: A RESTful Interface for Erlang Code using Webmachine

How does a resource function wire up to the web?

• There is a mapping between 30+ functions that you can put in a resource module.

• They map to states in the HTTP state machine that webmachine implements.

Page 20: A RESTful Interface for Erlang Code using Webmachine

Resource functions

• resource_exists• service_available• allowed_methods• content_types_provid

ed• etcetera

• These functions all have default values when absent.

• You only write functions when you needs something more.

Page 21: A RESTful Interface for Erlang Code using Webmachine

content_types_provided

• This function defines the types of content that the module can provide.

• It defaults to [{“text/html”,to_html}] .

• to_html is function name that creates the results.

• You might want this to return [{“text/html”,to_html}, {“json”,to_json}]

Page 22: A RESTful Interface for Erlang Code using Webmachine

A real resource function

Page 23: A RESTful Interface for Erlang Code using Webmachine

Actual results

Page 24: A RESTful Interface for Erlang Code using Webmachine

Questions?