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
120
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
250
Hexes, Charms and Spells
jadeallenx
0
150
3 > 2
jadeallenx
0
960
Idiomatic Erlang
jadeallenx
0
150
The Sharp Edges of Leaky Abstractions
jadeallenx
0
98
Sagas: Distributed Transactions Without Locks
jadeallenx
0
570
Before Unix: Early History of Time-Sharing
jadeallenx
0
210
Functional Programming Made Me a Better Perl Developer
jadeallenx
1
350
Assigning Meanings to Programs
jadeallenx
0
210
Other Decks in Technology
See All in Technology
Amazon Aurora のバージョンアップ手法について
smt7174
2
190
OPENLOGI Company Profile
hr01
0
60k
マルチアカウント環境における組織ポリシーについて まとめてみる
nrinetcom
PRO
2
110
プロダクト開発者目線での Entra ID 活用
sansantech
PRO
0
170
Oracle Database Technology Night #87-1 : Exadata Database Service on Exascale Infrastructure(ExaDB-XS)サービス詳細
oracle4engineer
PRO
1
230
Amazon Q Developerの無料利用枠を使い倒してHello worldを表示させよう!
nrinetcom
PRO
2
130
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
27
17k
AWSではじめる Web APIテスト実践ガイド / A practical guide to testing Web APIs on AWS
yokawasa
8
800
事業を差別化する技術を生み出す技術
pyama86
2
550
事業モメンタムを生み出すプロダクト開発
macchiitaka
0
110
20250307_エンジニアじゃないけどAzureはじめてみた
ponponmikankan
2
200
Snowflakeの開発・運用コストをApache Icebergで効率化しよう!~機能と活用例のご紹介~
sagara
1
540
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Designing for Performance
lara
605
68k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Building Adaptive Systems
keathley
40
2.4k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Gamification - CAS2011
davidbonilla
80
5.2k
The Invisible Side of Design
smashingmag
299
50k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
28
1.9k
Transcript
Introduction to Webmachine! ! ! Mark Allen! mrallen1@yahoo.com! @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?!