Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Created October 27, 2009 14:13
Show Gist options
  • Save leandrosilva/219585 to your computer and use it in GitHub Desktop.
Save leandrosilva/219585 to your computer and use it in GitHub Desktop.
A experience on rack
require 'sinatra-rack_app'
run Sinatra::Application
require 'sinatra'
get '/' do
puts ''
puts '-> request uri: /'
puts ''
env.each do |k, v|
puts "#{k} = #{v}"
end
puts ''
"Started up!"
end
get '/foo/:bar' do
puts '-> request uri: /foo/:bar'
"You asked for foo/#{params[:bar]}"
end
require 'rack'
require 'rack/rewindable_input'
# constants for environment variable -------------------------------------
RACK_VERSION = "rack.version"
RACK_INPUT = "rack.input"
RACK_ERRORS = "rack.errors"
RACK_MULTITHREAD = "rack.multithread"
RACK_MULTIPROCESS = "rack.multiprocess"
RACK_RUN_ONCE = "rack.run_once"
REQUEST_METHOD = "REQUEST_METHOD"
REQUEST_PATH = "REQUEST_PATH"
PATH_INFO = "PATH_INFO"
QUERY_STRING = "QUERY_STRING"
rewindable_input = ::Rack::RewindableInput.new(STDIN)
# setup environment variable ---------------------------------------------
env = Hash.new
env[RACK_VERSION] = [1, 0, 1]
env[RACK_INPUT] = rewindable_input
env[RACK_ERRORS] = STDERR
env[RACK_MULTITHREAD] = false
env[RACK_MULTIPROCESS] = true
env[RACK_RUN_ONCE] = false
env[REQUEST_METHOD] = "GET"
env[REQUEST_PATH] = "/"
env[PATH_INFO] = "/"
env[QUERY_STRING] = ""
# build application ------------------------------------------------------
app = Rack::Builder.new { eval ::File.read("sinatra-rack.ru") }.to_app
puts "started up: #{app}"
# process the request ----------------------------------------------------
puts "processing request: #{env[REQUEST_PATH]}"
status, headers, body = app.call env
rewindable_input.close
puts ""
puts "- status = #{status}"
puts "- headers = #{headers}"
puts "- body = #{body}"
$ruby sinatra-rack_startup.rb
started up: Sinatra::Application
processing request: /
-> request uri: /
rack.version = [1, 0, 1]
rack.input = #<Rack::RewindableInput:0x271da20>
rack.errors = #<IO:0x1aa4980>
rack.multithread = false
rack.multiprocess = true
rack.run_once = false
REQUEST_METHOD = GET
REQUEST_PATH = /
PATH_INFO = /
QUERY_STRING =
rack.request.query_string =
rack.request.query_hash = {}
rack.request.form_input = #<Rack::RewindableInput:0x271da20>
rack.request.form_hash = {}
rack.request.form_vars =
- status = 200
- headers = {"Content-Type"=>"text/html", "Content-Length"=>"11"}
- body = #<Rack::CommonLogger:0x26132c0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment