Answer the question
In order to leave comments, you need to log in
Rack work without middleware?
require "net/http"
require "stringio"
class Worker
def call(env)
request = Rack::Request.new(env)
case request.request_method
when "POST"
[200,{"Content-Type"=>"text/plain"},StringIO.new("")]
when "GET", "PUT", "DELETE", "OPTION", "HEAD"
[404,{"Content-Type"=>"text/plain"}, StringIO.new("Not Found")]
end
create_response(request.body.read)
end
def create_response(body)
uri = URI("server2")
res = Net::HTTP::Post.new(uri.request_uri)
res.content_type = 'text/plain'
Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(res, body) }
return [200,{"Content-Type"=>"text/plain"},StringIO.new("")] # если не возвращать эту строку rack'y - будет ошибка
end
end
run Worker.new
lambda { |env| [200,{"Content-Type"=>"text/plain"},StringIO.new("")] }
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question