Created
October 27, 2009 14:13
-
-
Save leandrosilva/219585 to your computer and use it in GitHub Desktop.
A experience on rack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra-rack_app' | |
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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