Slide 1

Slide 1 text

Building with Rack Alexander Clark

Slide 2

Slide 2 text

What is Rack?

Slide 3

Slide 3 text

?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

?

Slide 8

Slide 8 text

?

Slide 9

Slide 9 text

What does a Rack app look like? ?

Slide 10

Slide 10 text

What does a Rack app look like? GET /foo HTTP/1.1 Host: localhost:9292 Accept: text/html … ?

Slide 11

Slide 11 text

What does a Rack app look like? GET /foo HTTP/1.1 Host: localhost:9292 Accept: text/html … ? …

Slide 12

Slide 12 text

run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] }

Slide 13

Slide 13 text

run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] } Responds to #call HTTP Status code Headers Response body Run

Slide 14

Slide 14 text

run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] } Responds to #call Much HTTP Status code Such Headers Response body Wow Run

Slide 15

Slide 15 text

Let’s build something!

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Is it April Fools?

<%= @april_fools ? 'YES' : 'NO' %>

Slide 18

Slide 18 text

require 'erb' class MyRackApp def initialize @april_fools = Time.now.strftime('%m%d') == '0401' end def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end def view [ERB.new(template).result(binding)] end def template File.open('index.html.erb', 'r').read end end

Slide 19

Slide 19 text

require './my_rack_app' run MyRackApp.new

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

?

Slide 22

Slide 22 text

run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] } run MyRackApp.new ... def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end

Slide 23

Slide 23 text

run MyRackApp.new run Rails.application

Slide 24

Slide 24 text

Middleware

Slide 25

Slide 25 text

TL;DL

Slide 26

Slide 26 text

Further Reading • https://rack.github.io/ • http://alexander-clark.com/blog/building-a-rack-app/ • http://www.justinweiss.com/articles/a-web-server-vs- an-app-server/ • https://www.amberbit.com/blog/2011/07/13/ introduction-to-rack-middleware/

Slide 27

Slide 27 text

Thanks for Listening Alexander Clark atheclark alexander-clark.com alexander-clark