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
260
Hexes, Charms and Spells
jadeallenx
0
160
3 > 2
jadeallenx
0
1k
Idiomatic Erlang
jadeallenx
0
160
The Sharp Edges of Leaky Abstractions
jadeallenx
0
100
Sagas: Distributed Transactions Without Locks
jadeallenx
0
570
Before Unix: Early History of Time-Sharing
jadeallenx
0
220
Functional Programming Made Me a Better Perl Developer
jadeallenx
1
360
Assigning Meanings to Programs
jadeallenx
0
220
Other Decks in Technology
See All in Technology
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
53
31k
Кто отправит outbox? Валентин Удальцов, автор канала Пых
lamodatech
0
250
生成AIでwebアプリケーションを作ってみた
tajimon
2
120
ユーザーのプロフィールデータを活用した推薦精度向上の取り組み
yudai00
0
460
AIのAIによるAIのための出力評価と改善
chocoyama
0
430
2年でここまで成長!AWSで育てたAI Slack botの軌跡
iwamot
PRO
2
130
Amazon ECS & AWS Fargate 運用アーキテクチャ2025 / Amazon ECS and AWS Fargate Ops Architecture 2025
iselegant
13
4.1k
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
140
比起獨自升級 我更喜歡 DevOps 文化 <3
line_developers_tw
PRO
0
1.1k
ローカルLLMでファインチューニング
knishioka
0
120
kubellが挑むBPaaSにおける、人とAIエージェントによるサービス開発の最前線と技術展望
kubell_hr
1
390
In Praise of "Normal" Engineers (LDX3)
charity
2
1.2k
Featured
See All Featured
Being A Developer After 40
akosma
90
590k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Gamification - CAS2011
davidbonilla
81
5.3k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
The Pragmatic Product Professional
lauravandoore
35
6.7k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
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?!