Sinatra Rack And Middleware

82
sinatra, rack & middleware This is made “soon after” railsconf, so you’ll just have to deal with the railsconf references

description

How I learned to make rack middleware using Sinatra

Transcript of Sinatra Rack And Middleware

Page 1: Sinatra Rack And Middleware

sinatra, rack &middleware

This is made “soon after” railsconf, so you’ll just have to deal with the railsconf references

Page 2: Sinatra Rack And Middleware

sinatra, rack &middlewareBen Schwarz@benschwarzhttp://github.com/benschwarz

This is made “soon after” railsconf, so you’ll just have to deal with the railsconf references

Page 3: Sinatra Rack And Middleware

sinatra

Page 4: Sinatra Rack And Middleware

from the top

Page 5: Sinatra Rack And Middleware

... is fucking sweet

Page 6: Sinatra Rack And Middleware

DSL

Page 7: Sinatra Rack And Middleware

Built on RackMore on that later

Page 8: Sinatra Rack And Middleware

Blake Mizerany@bmizerany

Page 9: Sinatra Rack And Middleware

Ever wondered what Blake Mizerany looks like at 8 am after a huge fucking bender in “old” vegas?

Page 10: Sinatra Rack And Middleware
Page 11: Sinatra Rack And Middleware

Eyyeaahh.

Page 12: Sinatra Rack And Middleware

So, that DSL

Page 13: Sinatra Rack And Middleware

heres an example of the most simple sinatra application you could probably make. The ‘get’ referrers to the HTTP verb. You should probably recognise this from rails land. The next part is the “route”, here I’m just mapping the index / root. “erb :index” will render index.erb, sinatra also has haml support out of the box.

Page 14: Sinatra Rack And Middleware

heres an example of the most simple sinatra application you could probably make. The ‘get’ referrers to the HTTP verb. You should probably recognise this from rails land. The next part is the “route”, here I’m just mapping the index / root. “erb :index” will render index.erb, sinatra also has haml support out of the box.

Page 15: Sinatra Rack And Middleware

heres an example of the most simple sinatra application you could probably make. The ‘get’ referrers to the HTTP verb. You should probably recognise this from rails land. The next part is the “route”, here I’m just mapping the index / root. “erb :index” will render index.erb, sinatra also has haml support out of the box.

Page 16: Sinatra Rack And Middleware

heres an example of the most simple sinatra application you could probably make. The ‘get’ referrers to the HTTP verb. You should probably recognise this from rails land. The next part is the “route”, here I’m just mapping the index / root. “erb :index” will render index.erb, sinatra also has haml support out of the box.

Page 17: Sinatra Rack And Middleware

a ‘post’ example. you might notice the params hash is nothing new if you’ve come from rails land.

Page 18: Sinatra Rack And Middleware

a ‘post’ example. you might notice the params hash is nothing new if you’ve come from rails land.

Page 19: Sinatra Rack And Middleware

a ‘post’ example. you might notice the params hash is nothing new if you’ve come from rails land.

Page 20: Sinatra Rack And Middleware

a ‘post’ example. you might notice the params hash is nothing new if you’ve come from rails land.

Page 21: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 22: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 23: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 24: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 25: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 26: Sinatra Rack And Middleware

a slightly more advanced example. Here I create a mime type of :json and use before (read: like before_filter) to set the :json content type before all routes. The .to_json method isn’t Sinatra magic. In this case it comes from using datamappers’ aggrigates plugin, activerecord of course has this built in.

Page 27: Sinatra Rack And Middleware

This is called “Classic”Sinatra application style

This means that the get and post (read: your application) is defined at the top level

Page 28: Sinatra Rack And Middleware

Apps can also be defined in another way.Modular apps are defined within their own namespace / class.They inherit from Sinatra::Base

Page 29: Sinatra Rack And Middleware

Apps can also be defined in another way.Modular apps are defined within their own namespace / class.They inherit from Sinatra::Base

Page 30: Sinatra Rack And Middleware

Thats a “Modular” Sinatra application

I’ll get into modular apps a little more later

Page 31: Sinatra Rack And Middleware

Running it

So by now, if you’re unfamiliar with rack or sinatra you’ll might be thinking, “but we just got passenger, ruby hosting has only now become ‘easy’”

Page 32: Sinatra Rack And Middleware

Development

Page 33: Sinatra Rack And Middleware

you could use the `rackup` command, but instead you should use ‘shotgun’ by Ryan Tomayko, it handles application reloading (which is not present within the sinatra codebase)

Page 34: Sinatra Rack And Middleware

Ryan Tomayko@rtomayko

Page 35: Sinatra Rack And Middleware

Ever wondered what Ryan Tomayko looks like at 8 am after a huge fucking bender in “old” vegas?

Page 36: Sinatra Rack And Middleware

Not much earlier (5am) Ryan sent his wife an email saying “Melbourne, we’re going”. She called in the morning to find out if he was mid-way across the pacific or not.

Page 37: Sinatra Rack And Middleware

Production

Page 38: Sinatra Rack And Middleware

Drop in a config.ru to your application root

config.Rack-Up, rack uses this to load and configure rack applications and middleware

Page 39: Sinatra Rack And Middleware

this is a simple config.ru file. its for a ‘classic’ application

Page 40: Sinatra Rack And Middleware

For those “Modular” applications

Page 41: Sinatra Rack And Middleware

a rackup file (config.ru) example for a modular application

Page 42: Sinatra Rack And Middleware

Then drop it under passenger. Done

Of course you’ll need to read some docs, but its so trivial its not even worth mentioning

Page 43: Sinatra Rack And Middleware

Heres the part where I hock my own warez

Just some assorted things that I have found really fun or interesting in the last few months

Page 44: Sinatra Rack And Middleware

AmnesiaStatistics for memcached instances

Page 45: Sinatra Rack And Middleware

So thats all your hits, misses and basic stats to tell you what the hell your memcached instances are doing. If they’re getting smashed. Etc. I know iseekgolf, a large australian golfing website have used it, along with some engineyard customers. github and flickr also checked it out which was pretty cool. Amnesia was a 2 session application, one for implementation, the next to add the graphs etc. I think I spent about 5 hours total on it.

Page 46: Sinatra Rack And Middleware

MunchRecipes from websites re-represented

I showed this last month, its pretty simple so I’ll gloss over it.

Page 47: Sinatra Rack And Middleware

Basically a aggregate search engine for cooking sites

Page 48: Sinatra Rack And Middleware

Postie

Postie was originally a rails app, ported to merb

Page 49: Sinatra Rack And Middleware

Pat Allan@pat

Pat allan wrote it

Page 50: Sinatra Rack And Middleware

PostieA Rack middleware to provide postcode services

I decided to make the next natural progression and re-write it as a rack middleware

Page 51: Sinatra Rack And Middleware

It has its own datamapper based sqlite backend. When a new build comes out, new data will be installed along with the gem. Its quite small though.

Page 52: Sinatra Rack And Middleware

Here is the basic api

Page 53: Sinatra Rack And Middleware

Here is the basic api

Page 54: Sinatra Rack And Middleware

Here is the basic api

Page 55: Sinatra Rack And Middleware
Page 56: Sinatra Rack And Middleware
Page 57: Sinatra Rack And Middleware

Hold on, rack middleware?

If you thought we were talking about sinatra, well, you’re right.

Page 58: Sinatra Rack And Middleware

So how does that work?

Page 59: Sinatra Rack And Middleware

Sinatra runs on Rack

Page 60: Sinatra Rack And Middleware

Sinatra (wolf) Rack (sheeps clothing)

This is a strange analogy. I don’t know.

Page 61: Sinatra Rack And Middleware

So when you go and define you app in “modular style”, it can be used as a rack middleware.

Page 62: Sinatra Rack And Middleware

a config.ru file to run your application as middleware, note the use of “use”.

Page 63: Sinatra Rack And Middleware

a config.ru file to run your application as middleware, note the use of “use”.

Page 64: Sinatra Rack And Middleware

a config.ru file to run your application as middleware, note the use of “use”.

Page 65: Sinatra Rack And Middleware

In rails land, you do it like this

Page 66: Sinatra Rack And Middleware

In rails land, you do it like this

Page 67: Sinatra Rack And Middleware

In rails land, you do it like this

Page 68: Sinatra Rack And Middleware

In rails land, you do it like this

Page 69: Sinatra Rack And Middleware

Selected middlewares

Page 70: Sinatra Rack And Middleware

Rack::Cache

Page 71: Sinatra Rack And Middleware

and again, its made by Ryan Tomayko

Page 72: Sinatra Rack And Middleware

Reverse proxyLike Squid or Varnish, but small and simple

It’ll set correct http headers. Thats really important. The main basis of this is that your application pages can be cached by rack::cache and stop requests from even having to hit your ruby process.

Page 73: Sinatra Rack And Middleware

JSON-P

Json-p is for when you want to get callbacks for your json / ajax requests from your server. The data will be returned wrapped within a callback method. This can make writing a javascript based interface much faster and easier to implement.

Page 74: Sinatra Rack And Middleware

instead of doing something like this

Page 75: Sinatra Rack And Middleware

you can add a callback parameter

Page 76: Sinatra Rack And Middleware

so instead of getting some result like we did earlier, a raw javascript / json “string” in the browser that needs to be eval’d and looked after

Page 77: Sinatra Rack And Middleware

you can get it back like this, it calls your method and can be handled more cleanly

Page 78: Sinatra Rack And Middleware

JSON-Ppart of rack-contrib

Json-p is for when you want to get callbacks for your json / ajax requests from your server. The data will be returned wrapped within a callback method. This can make writing a javascript based interface much faster and easier to implement.

Page 79: Sinatra Rack And Middleware

Hancock

The last one I’ll show is “hancock”

Page 80: Sinatra Rack And Middleware

A REAL implementation of single-sign-on

Engine yard are using this internally and I really suggest that you read the code. There are some videos available online and it stands as the best example of sinatra based rack middleware. Hancock was the only real implementation of a sinatra based rack middleware that he sinatra committers could point me at. I used it as a basis for learning how to pack postie together.

Page 81: Sinatra Rack And Middleware

Thanks

Special Thanks to Blake and Ryan for being good sports.