Sinatra Rack And Middleware

100 %
0 %
Information about Sinatra Rack And Middleware
Technology

Published on May 30, 2009

Author: benschwarz

Source: slideshare.net

Description

How I learned to make rack middleware using Sinatra

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

sinatra, rack & middleware Ben Schwarz @benschwarz http://github.com/benschwarz This is made “soon after” railsconf, so you’ll just have to deal with the railsconf references

sinatra

from the top

... is fucking sweet

DSL

Built on Rack More on that later

Blake Mizerany @bmizerany

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

Eyyeaahh.

So, that DSL

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.

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.

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.

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.

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

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

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

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

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.

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.

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.

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.

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.

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.

This is called “Classic” Sinatra application style This means that the get and post (read: your application) is defined at the top level

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

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

Thats a “Modular” Sinatra application I’ll get into modular apps a little more later

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’”

Development

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)

Ryan Tomayko @rtomayko

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

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.

Production

Drop in a config.ru to your application root config.Rack-Up, rack uses this to load and configure rack applications and middleware

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

For those “Modular” applications

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

Then drop it under passenger. Done Of course you’ll need to read some docs, but its so trivial its not even worth mentioning

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

Amnesia Statistics for memcached instances

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.

Munch Recipes from websites re-represented I showed this last month, its pretty simple so I’ll gloss over it.

Basically a aggregate search engine for cooking sites

Postie Postie was originally a rails app, ported to merb

Pat Allan @pat Pat allan wrote it

Postie A Rack middleware to provide postcode services I decided to make the next natural progression and re-write it as a rack 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.

Here is the basic api

Here is the basic api

Here is the basic api

Hold on, rack middleware? If you thought we were talking about sinatra, well, you’re right.

So how does that work?

Sinatra runs on Rack

Sinatra (wolf) Rack (sheeps clothing) This is a strange analogy. I don’t know.

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

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

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

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

In rails land, you do it like this

In rails land, you do it like this

In rails land, you do it like this

In rails land, you do it like this

Selected middlewares

Rack::Cache

and again, its made by Ryan Tomayko

Reverse proxy Like 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.

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.

instead of doing something like this

you can add a callback parameter

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

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

JSON-P part 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.

Hancock The last one I’ll show is “hancock”

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.

Thanks Special Thanks to Blake and Ryan for being good sports.

Thanks http://sinatrarb.com http://twitter.com/bmizerany http://twitter.com/rtomayko http://github.com/benschwarz/amnesia http://github.com/benschwarz/munch http://github.com/benschwarz/postie http://github.com/rtomayko/shotgun http://github.com/rack/rack-contrib http://github.com/atmos/hancock/tree/master http://github.com/atmos/hancock-client/tree/master http://en.wikipedia.org/wiki/Single_sign-on Special Thanks to Blake and Ryan for being good sports.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Sinatra: README

Not only is Sinatra able to use other Rack middleware, any Sinatra application can in turn be added in front of any Rack endpoint as middleware itself.
Read more

Faking Sinatra with Rack and Middleware - YouTube

Sinatra has a really nice DSL. You can fake the basics of the DSL with some simple Rack middleware created by metaprogramming
Read more

Sinatra Recipes - Middleware - Rack Auth Basic And Digest

Did we miss something? It's very possible we've left something out, that's why we need your help! This is a community driven project after all.
Read more

ruby on rails - Sinatra rack middleware hijacks '/' root ...

I'm trying to use a Sinatra app as middleware in my Rails app. I've tested a basic Sinatra app in the /lib folder of the Rails app, used the middleware and ...
Read more

Introduction to Rack middleware - Ruby on Rails and Elixir ...

Introduction to Rack middleware 2011-07-13 ... Today, Ruby on Rails, Ramaze, Sinatra and many others use it by default to talk to web servers, ...
Read more

Sinatra app as rack middleware TimesOut Rails 3 - Stack ...

While in the Rails development environment, I am attempting to add a Sinatra app as a middleware. The Sinatra app uses the geoip gem that processes a user ...
Read more

4. Modular Applications - Sinatra: Up and Running [Book]

Another use case is combining multiple Sinatra applications in a single process or using Sinatra as Rack middleware. ... Rack::Mount and Sinatra have ...
Read more

Sinatra: Documentation

Detailed documentation on all of Sinatra’s built-in settings and using set ... Testing Sinatra with Rack:: ... Sinatra, rack and middleware.
Read more

Sinatra Recipes - Middleware - Rack Commonlogger

Did we miss something? It's very possible we've left something out, that's why we need your help! This is a community driven project after all.
Read more