Created
April 27, 2010 09:48
-
-
Save Stanley/380553 to your computer and use it in GitHub Desktop.
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 'em-proxy' | |
Proxy.start(:host => "127.0.0.1", :port => 8080) do |conn| | |
@start = Time.now | |
@data = Hash.new("") | |
conn.server :prod, :host => "db.wasiutynski.net", :port => 80 # production, will render resposne | |
conn.server :test, :host => "127.0.0.1", :port => 5984 # testing, internal only | |
conn.on_data do |data| | |
# rewrite User-Agent | |
data.gsub(/User-Agent: .*?\r\n/, "User-Agent: em-proxy/0.1\r\n") | |
end | |
conn.on_response do |server, resp| | |
# only render response from production | |
@data[server] += resp | |
resp if server == :prod | |
end | |
conn.on_finish do |name| | |
p [:on_finish, name, Time.now - @start] | |
p @data | |
end | |
end | |
# $ ruby duplex_proxy.rb & | |
# $ curl http://localhost:8080/krug | |
# {"db_name":"krug","doc_count":3,"doc_del_count":0,"update_seq":3,"purge_seq":0,"compact_running":false,"disk_size":12377,"instance_start_time":"1272355167598548","disk_format_version":5} | |
# [:on_finish, :prod, 0.08536626] | |
# {:test=>"HTTP/1.1 200 OK\r\nServer: CouchDB/0.11.0 (Erlang OTP/R13B)\r\nDate: Tue, 27 Apr 2010 09:44:46 GMT\r\nContent-Type: text/plain;charset=utf-8\r\nContent-Length: 189\r\nCache-Control: must-revalidate\r\n\r\n{\"db_name\":\"krug\",\"doc_count\":2,\"doc_del_count\":1,\"update_seq\":14,\"purge_seq\":0,\"compact_running\":false,\"disk_size\":180313,\"instance_start_time\":\"1272353276038978\",\"disk_format_version\":5}\n", | |
# :prod=>"HTTP/1.1 200 OK\r\nServer: nginx/0.5.36\r\nDate: Tue, 27 Apr 2010 09:44:47 GMT\r\nContent-Type: text/plain;charset=utf-8\r\nConnection: keep-alive\r\nContent-Length: 187\r\nCache-Control: must-revalidate\r\n\r\n{\"db_name\":\"krug\",\"doc_count\":3,\"doc_del_count\":0,\"update_seq\":3,\"purge_seq\":0,\"compact_running\":false,\"disk_size\":12377,\"instance_start_time\":\"1272355167598548\",\"disk_format_version\":5}\n"} | |
# [:on_finish, :test, 0.085606746] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment