Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Webmachine
Search
Jade Allen
October 16, 2013
Technology
0
110
Introduction to Webmachine
Webmachine is a toolkit to build REST APIs implemented in Erlang.
Jade Allen
October 16, 2013
Tweet
Share
More Decks by Jade Allen
See All by Jade Allen
All About Regular Expressions
jadeallenx
1
230
Hexes, Charms and Spells
jadeallenx
0
140
3 > 2
jadeallenx
0
830
Idiomatic Erlang
jadeallenx
0
130
The Sharp Edges of Leaky Abstractions
jadeallenx
0
92
Sagas: Distributed Transactions Without Locks
jadeallenx
0
550
Before Unix: Early History of Time-Sharing
jadeallenx
0
180
Functional Programming Made Me a Better Perl Developer
jadeallenx
1
340
Assigning Meanings to Programs
jadeallenx
0
180
Other Decks in Technology
See All in Technology
Cloudflare WorkersがPythonに対応したので試してみた
miura55
0
190
「できる!」を増やすGitHub Copilot活用法 / How to use GitHub Copilot to expand your possibilities
sansan_randd
1
240
OPENLOGI Company Profile
hr01
0
45k
生成AIと産業向けソフトウェアの自動生成 〜 ハノーバーメッセ2024より〜
kioto
2
420
サービス開発におけるVue3とTypeScriptの親和性について
tsukuha
10
1.8k
TailwindCSSでUIライブラリを作る際のハマりどころ
shuta13
0
230
LINEヤフーのウェブアクセシビリティ
lycorptech_jp
PRO
2
170
Taking Flight with Tailwind CSS
opdavies
0
4.3k
SWC Transformerから見るTypeScript関数記述ベストプラクティス
fujiyamaorange
1
170
5分で分かる(かもしれない) Vector engine for OpenSearch Serverless
tsukuboshi
1
400
.NET GraphQL Client のリアル
sansantech
PRO
1
220
Domain-driven Design: A Complete Example
ewolff
2
250
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
515
39k
The Cost Of JavaScript in 2023
addyosmani
21
4k
Facilitating Awesome Meetings
lara
43
5.6k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
242
1.2M
[RailsConf 2023] Rails as a piece of cake
palkan
28
4.1k
The Power of CSS Pseudo Elements
geoffreycrofte
62
5k
Atom: Resistance is Futile
akmur
260
25k
A designer walks into a library…
pauljervisheath
201
23k
The Language of Interfaces
destraynor
151
23k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Mobile First: as difficult as doing things right
swwweet
217
8.6k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Transcript
Introduction to Webmachine! ! ! Mark Allen!
[email protected]
! @bytemeorg! http://byte-me.org!
https://github.com/mrallen1!
HTTP is complicated!
None
Abstraction!
Toolkit!
A gentle introduction! to Erlang!
Sincerely flattered!
dj-webmachine! (python)!
webmachine.rb! (ruby)!
webmachine.js! (node)!
liberator! (clojure)!
scalamachine! (scala)!
Framework!
Framework!
None
Hello World! Resource!
-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}.
-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}.
-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}.
Conditional GET!
-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}.
Encoding Support!
-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}.
None
Content Negotiation!
-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}. %% ...
None
Resource! Implementation! Pattern!
f(Req, State) -> {RetVal, Req, State}.
Manipulating! Requests or Responses!
wrq:get_req_header(HeaderName, Req) -> 'undefined' | HeaderValue
wrq:set_resp_header( HeaderName, HeaderValue, Req) -> NewReq.
to_json(Req, State) -> {mochijson2:encode({struct, [{greeting, list_to_binary(State)}]} ), wrq:set_resp_header( "Content-Type", "application/json",
Req), State}.
None
Visual Debugging!
None
curl -v -Haccept:application/xml 127.0.0.1:8000
Basic URL Dispatch!
{["a"], some_resource, []}
{["a"], some_resource, []}
{["a"], some_resource, []}
Fancy URL Dispatch!
% The atom '*' is a wildcard {["a", '*'], some_resource,
[]} % matches /a/b/c or /a/b/c/d
% bind 2nd element to 'foo' {["a", foo], some_resource, []}
% retrieve with % wrq:path_info(foo, Req) % URL of /a/b returns "b"
% bind 2nd element to 'foo' {["a", foo], some_resource, []}
% /a/b/c/d => 404 Not Found
% dispatch /a/b/c/d {["a", foo, '*'], some_resource, []}
Handling POST!
• allowed_methods/2 • post_is_create/2 • create_path/2 • content_types_accepted/2 • handler
functions!
Other cool stuff:! • Streaming requests/ responses! • File uploads!
Resources:! • https://github.com/basho/webmachine! • https://github.com/mrallen1/wm_hello! • https://speakerdeck.com/mrallen1/ intro_to_webmachine!
Thank you!! ! Questions?!