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
240
Hexes, Charms and Spells
jadeallenx
0
150
3 > 2
jadeallenx
0
920
Idiomatic Erlang
jadeallenx
0
140
The Sharp Edges of Leaky Abstractions
jadeallenx
0
95
Sagas: Distributed Transactions Without Locks
jadeallenx
0
560
Before Unix: Early History of Time-Sharing
jadeallenx
0
190
Functional Programming Made Me a Better Perl Developer
jadeallenx
1
350
Assigning Meanings to Programs
jadeallenx
0
200
Other Decks in Technology
See All in Technology
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.3k
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
120
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
590
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
140
複雑なState管理からの脱却
sansantech
PRO
1
140
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
310
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
95
Unsuck your backbone
ammeep
668
57k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
What's new in Ruby 2.0
geeforr
343
31k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
GitHub's CSS Performance
jonrohan
1030
460k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Speed Design
sergeychernyshev
25
620
Designing for humans not robots
tammielis
250
25k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
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?!