Ruby on rack #1-first close contact with rack

Source: Internet
Author: User

Http://article.yeeyan.org/view/52654/18184

Rack provides interfaces between the Web server and the ruby development framework. It frees framework writers from the heavy burden of writing a processing module for each web server, saving a lot of repetitive work.



The Ruby community shows new frameworks almost every week, but rack is not getting enough attention in these frameworks. It should be followed. In addition, to fully utilize rack, a better public interface will be available after the next stable version of rails 2.2.

Rack was initially inspired by Python's wsgi. Due to its simplicity and accuracy, rack quickly became a practical web application/server interface in the Ruby community. You may want to see the rack introduction from Christian neukirchen, the father of rack.

What is rack?

-Rack API docs

In fact, you can divide "rack" into two parts:

Rack rules

The Rack rule specifies how an rack application and Webserver interact accurately:

-Rack Specification

This is just a brief description. You can see more details here.

Strictly speaking, you do not need this rack gem to write an rack ready application. This is only for Standardization:

Rack gem

Rack gem is a collection of public and auxiliary classes, which makes every rack app developer easier. It includes basic request, response, cookies, and sessions implementations, as well as a lot of useful middleware. To install rack gem, you only need:

$ sudo gem install rack
Summary
  • Rack is a framework that supports your own Ruby framework.
  • Rack provides an interface between different web servers and your frameworks/applications. Make it easier for your framework/application to be compatible with any rack-supported Web servers-phusion passion, litespeed, mongrel, thin, ebb, and webrick.
  • Rack reduces your costs. You can get request, response, cookies, Params and sessions for free.
  • No class conflicts make it possible to use multiple frameworks in one application. Integration of rails and Sinatra is a good example.
  • Middleware! Think about rails before_filter/after_filter, which can be reused in different frameworks that support rack. For example, the same anti-spamming rack middleware can be used in your rails application, Sinatra application, and custom rack application.
Example

Let's use mongrel, starting with an example that may be the smallest rack application.

1
2
3
4
5
6
7
8
9
10
require 'rubygems'
require 'rack'

class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end

Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292

The above code transmits a helloworld object to the rack processor of mongrel, and port 9292 is specified.


TheHelloworldThe object complies with the rack specification:

  1. The call method only includes one env parameter.
  2. Call ()Method returns an array containing the three values [http_status_code, response_headers_hash, body]

That's enough! Run the script and enter http: // localhost: 9292 in the browser. Then you will see this dazzling "Hello rack !" Information.

So, since it is a ruby process that responds to call (), why not use a proc instead? So there is no reason not to do this:

1
2
3
4
require 'rubygems
require 'rack'

Rack::Handler::Mongrel.run proc {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}, :Port => 9292

Another common mode is to useMethod (: Something)This will return a method class object:

1
2
3
4
5
6
7
8
require 'rubygems'
require 'rack'

def application(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end

Rack::Handler::Mongrel.run method(:application), :Port => 9292

Take away your very slow "Hello World" performances. You can't write a "hello World" Ruby application faster than this one.

Rack it up'

As I said before, rack gem brings a lot of useful things to rack app developers.RackupIs one of them. In the previous example, I used the mongrel processor directly.Rack: Handler: mongrelAnd even hard-coded port numbers. With rackup, all these tasks are configurable .! But with rackup, you need to use a rackup configuration file to support it. In the previous example, the configuration file is a bit like:

1
2
# config.ru
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}

You only need one line. According to the Conventions, you should use. ru as the extension of a rackup configuration file. Supports runningRackobjectYou need:

$ rackup config.ru

However, by default, rackup starts a service on port 9292. However, you can use a-p parameter to specify the port number. You can see more help:

$ rackup --help


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.