Slide 1

Slide 1 text

webmachine github /edgurgel twitter /edgurjas gurgel.me

Slide 2

Slide 2 text

HTTP • URI • Method • Header • Body • Status Codes Easy, right?

Slide 3

Slide 3 text

Headers • Accept • Accept-Charset • Accept-Features • Accept-Encoding • Accept-Language • Accept-Ranges • Access-Control-Allow-Credentials • Access-Control-Allow-Origin • Access-Control-Allow-Methods • Access-Control-Allow-Headers • Access-Control-Max-Age • Access-Control-Expose-Headers • Access-Control-Request-Method • Access-Control-Request-Headers • Age • Allow • Alternates • Authorization • Cache-Control • Connection • Content-Encoding • Content-Language • Content-Length • Content-Location • Content-MD5 • Content-Range • Content-Security-Policy • Content-Type • Cookie • DNT • Date • ETag • Expect • Expires • From • Host • If-Match • If-Modified-Since • If-None-Match • If-Range • If-Unmodified-Since • Last-Event-ID

Slide 4

Slide 4 text

Headers • Last-Modified • Link • Location • Max-Forwards • Negotiate • Origin • Pragma • Proxy-Authenticate • Proxy-Authorization • Range • Referer • Retry-After • Sec-Websocket-Extensions • Sec-Websocket-Key • Sec-Websocket-Origin • Sec-Websocket-Protocol • Sec-Websocket-Version • Server • Set-Cookie • Set-Cookie2 • Strict-Transport-Security • TCN • TE • Trailer • Transfer-Encoding • Upgrade • User-Agent • Variant-Vary • Vary • Via • Warning • WWW-Authenticate • X-Content-Duration • X-Content-Security-Policy • X-DNSPrefetch-Control • X-Frame-Options • X-Requested-With

Slide 5

Slide 5 text

Status Codes • 100 Continue • 101 Switching Protocol • 200 OK • 201 Created • 202 Accepted • 203 Non-Authoritative Information • 204 No Content • 205 Reset Content • 206 Partial Content • 300 Multiple Choice • 301 Moved Permanently • 302 Found • 303 See Other • 304 Not Modified • 305 Use Proxy • 307 Temporary Redirect • 308 Permanent Redirect • 400 Bad Request • 401 Unauthorized • 402 Payment Required • 403 Forbidden • 404 Not Found • 405 Method Not Allowed • 406 Not Acceptable • 407 Proxy Authentication Required • 408 Request Timeout • 409 Conflict • 410 Gone • 411 Length Required • 412 Precondition Failed • 413 Request Entity Too Large • 414 Request-URI Too Long • 415 Unsupported Media Type • 416 Requested Range Not Satisfiable • 417 Expectation Failed • 500 Internal Server Error • 501 Not Implemented • 502 Bad Gateway • 503 Service Unavailable • 504 Gateway Timeout • 505 HTTP Version Not Supported

Slide 6

Slide 6 text

Processing Request Response

Slide 7

Slide 7 text

Rack class SomeMiddleware def initialize(app) @app = app end def call(env) # ... [status, headers, body] end end

Slide 8

Slide 8 text

Middleware-based class ApplicationController < ActionController::Base # ... before_filter :set_current_user_for_logs before_filter :set_locale before_filter :set_mobile_view before_filter :inject_preview_style before_filter :disable_customization before_filter :block_if_readonly_mode before_filter :authorize_mini_profiler before_filter :preload_json before_filter :check_xhr before_filter :redirect_to_login_if_required # ... end

Slide 9

Slide 9 text

Middleware-based • Order matters • Blind up, blind down • Hard to reuse • Env is a pile of data • Black box is a lie

Slide 10

Slide 10 text

Protocol HTTP 1.1 RFCs 7230, 7231, 7232, 7233, 7234,7235 Finite State Machine

Slide 11

Slide 11 text

Webmachine

Slide 12

Slide 12 text

Webmachine

Slide 13

Slide 13 text

Webmachine

Slide 14

Slide 14 text

Describe your resource • resource_exists? • service_available? • is_authorized? • forbidden? • malformed_request? • content_types_provided • content_types_accepted • moved_temporarily? • moved_permanently? • create_path • known_methods • last_modified • expires • allowed_methods • generate_etag • languages_provided • charsets_provided • encodings_provided

Slide 15

Slide 15 text

class PageResource < Webmachine::Resource def allowed_methods ['GET', 'HEAD', 'PUT'] end def resource_exists? page end def content_types_accepted [['application/json', :from_json]] end def content_types_provided [['text/html', :as_html]] end def from_json params = JSON.parse(request.body.to_s) redis.set id, "#{params['title']}#{params['body']}" end def as_html page end private def page @page ||= redis.get id end def id request.path_info[:id] end end

Slide 16

Slide 16 text

Example!

Slide 17

Slide 17 text

So? • Descriptive • Consistent behaviour • Scalable • Unit testable • Customisable

Slide 18

Slide 18 text

Implementations • Erlang (primary) - basho/webmachine! • Ruby - seancribbs/webmachine-ruby • Clojure - clojure-liberator/liberator • Haskell - larrytheliquid/Lemmachine • Javascript - tautologistics/nodemachine