background preloader

Rack as Glue

Facebook Twitter

Rack is a glue system for ruby web apps

Rack app with uri and HTTP specific responses · rack/rack Wiki. Goal Show how to write a simple Rack application responding differently if the request is a POST or a GET request. Also show how to use the map method to implement a simple router. Code Save the following code in a file named: rack_example.ru The code is pretty straight forward, but let me walk you through it nonetheless. We have an Idea class which is just there for the demo. At the bottom of the file, we can see that I'm mapping incoming requests for the '/ideas' uri to an instance of IdeaAPI.

The IdeaAPI class implements the Rack protocol by providing a call(env) method which gets triggered when the request is dispatched to an instance of itself. Notes In real life, you probably don't want to raise an exception and rescue it. Tutorials · rack/rack Wiki. (tutorial) rackup howto · rack/rack Wiki. Rackup How-To By Sam Roberts Config File Syntax The config file is config.ru if none is specified. Handling of config files depends on whether it’s .ru, or something else.

It’s important to define the application that rackup will run correctly; failure to do so will result in mysterious runtime errors! .ru: The config file is treated as if it is the body of app = Rack::Builder.new { ... config ... }.to_app Also, the first line starting with #\ is treated as if it was options, allowing rackup arguments to be specified in the config file. #\ -w -p 8765 use Rack::Reloader, 0 use Rack::ContentLength app = proc do |env| [ 200, {'Content-Type' => 'text/plain'}, ["a"] ] end run app Would run with Ruby warnings enabled, and request port 8765 (which will be ignored unless the server supports the :Port option). .rb, etc: The config file is required.

The name of the constant should be config file’s base name, stripped of a trailing .rb (if present), and captitalized. Auto-Selection of a Server. Using and scaling Rack and Rack-based middleware. Tutorials · rack/rack Wiki. Using and scaling Rack and Rack-based middleware. Alexch/Off-The-Rails. In a World of Middleware, Who Needs Monolithic Applications? - Jon Crosby - MountainWest RubyConf 2009. Presentations · rack/rack Wiki. Presentations · rack/rack Wiki. List of Middleware · rack/rack Wiki. Home · rack/rack Wiki.

Chris blogs: Introducing Rack. Dabbling in my own web framework experiments, I noticed that there is a lot of code duplication among frameworks since they essentially all do the same things. And still, every Ruby web framework developer is writing his own handlers for every webserver he wants to use. Hopefully, the framework users are satisfied with that choice. However, in essence, dealing with HTTP is rather easy. In the end, you get a request and return a response. Let’s do the easiest thing possible: The canonical format of a HTTP request probably is a hash of a CGI-like environment (that’s what most frameworks I’ve looked at deal with, internally), and a response consists of three parts: a status, a set of headers, and a body. This could be easily mapped onto a method call in Ruby, looking like this: class HelloWorld def call(env) [200, {"Content-Type" => "text/plain"}, ["Hello world!

"]] You’ve just seen the most simple Rack application. What do you gain if your web framework/server/application supports Rack? 'Rack Documentation' Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

The exact details of this are described in the Rack specification, which all Rack applications should conform to. Supported web servers The included handlers connect all kinds of web servers to Rack: MongrelEventedMongrelSwiftipliedMongrelWEBrickFCGICGISCGILiteSpeedThin These web servers include Rack handlers in their distributions: EbbFuzedGlassfish v3Phusion Passenger (which is mod_rack for Apache and for nginx)PumaRainbows! Any valid Rack app will run the same on all these handlers, without changing anything. Supported web frameworks These frameworks include Rack adapters in their distributions: Available middleware Convenience rack-contrib github.com/rack/rack-contrib rackup Quick start Or: Module: Rack. File: SPEC ['Rack Documentation'] This specification aims to formalize the Rack protocol. You can (and should) use Rack::Lint to enforce it. When you develop middleware, be sure to add a Lint before and after to catch all mistakes.

A Rack application is a Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: The status, the headers, and the body. The Environment The environment must be an instance of Hash that includes CGI-like headers. In addition to this, the Rack environment must include these Rack-specific variables: Additional environment specifications have approved to standardized middleware APIs. The server or the application can store their own data in the environment, too. The Input Stream The input stream is an IO-like object which contains the raw HTTP POST data. Gets must be called without arguments and return a string, or nil on EOF.read behaves like IO#read.

The Error Stream Hijacking Request (before status) If rack.hijack? Rack: a Ruby Webserver Interface.

Pow