Rack::Static

Véase

  1. Documentación de Rack::Static
  2. Este ejemplo: rack-static-example en GitHub
  3. Código fuente de Rack::Static en GitHub

Ejemplo

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ tree
.
|--- README
|--- README.md
|--- Rakefile
|--- config.ru
|--- myapp.rb
`---- public
   `--- index.html

1 directory, 6 files
[~/local/src/ruby/sinatra/rack/rack-static(master)]$ cat public/index.html 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ cat config.ru 
require './myapp'

use Rack::Static, :urls => ["/public"]

run MyApp.new

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ cat Rakefile 
task :default => :server

desc "run server"
task :server do
  sh "rackup"
end

desc "run client via curl"
task :client do
  sh "curl -v localhost:9292"
end

desc "access to static file"
task :index do
  sh "curl -v localhost:9292/public/index.html"
end

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ cat myapp.rb 
# my_app.rb
#
class MyApp
  def call env
    [200, {"Content-Type" => "text/html"}, ["Hello SYTW!"]] 
  end
end

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ rake client
curl -v localhost:9292
* About to connect() to localhost port 9292 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 9292 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:9292
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: close
< Server: thin 1.5.1 codename Straight Razor
< 
* Closing connection #0
Hello SYTW!

[~/local/src/ruby/sinatra/rack/rack-static(master)]$ rake index
curl -v localhost:9292/public/index.html
* About to connect() to localhost port 9292 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 9292 (#0)
> GET /public/index.html HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
> Host: localhost:9292
> Accept: */*
> 
< HTTP/1.1 200 OK
< Last-Modified: Thu, 03 Oct 2013 08:24:43 GMT
< Content-Type: text/html
< Content-Length: 227
< Connection: keep-alive
< Server: thin 1.5.1 codename Straight Razor
< 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

* Connection #0 to host localhost left intact
* Closing connection #0

El comando rackup

rackup converts the supplied rack config file to an instance of Rack::Builder. In short, rack config files are evaluated within the context of a Rack::Builder object.

Rackup also has a use method that accepts a middleware. Let us use one of Rack’s built-in middleware.

[~/sinatra/rackup/middleware]$ cat config.ru 
require './myapp'
require './myrackmiddleware'
use Rack::Reloader
use MyRackMiddleware
run MyApp.new

[~/sinatra/rackup/middleware]$ cat myapp.rb 
# myapp.rb
class MyApp
  def call(env)
    [200, {"Content-Type" => "text/html"}, ["Hello Rack Participants from across the globe"]] 
  end
end

[~/sinatra/rackup/middleware]$ cat myrackmiddleware.rb 
class MyRackMiddleware
  def initialize(appl)
    @appl = appl
  end
  def call(env)
    status, headers, body = @appl.call(env)
    append_s = "... greetings from RubyLearning!!"
    [status, headers, body << append_s]
  end  
end

Véase



Subsecciones
Casiano Rodriguez León 2015-01-07