[英]Ruby AMQP under Thin HTTP server
我正在运行一个简单的瘦服务器,该服务器将一些消息发布到不同的队列中,代码如下所示:
require "rubygems"
require "thin"
require "amqp"
require 'msgpack'
app = Proc.new do |env|
params = Rack::Request.new(env).params
command = params['command'].strip rescue "no command"
number = params['number'].strip rescue "no number"
p command
p number
AMQP.start do
if command =~ /\A(create|c|r|register)\z/i
MQ.queue("create").publish(number)
elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
end
end
[200, {'Content-Type' => "text/plain"} , command ]
end
Rack::Handler::Thin.run(app, :Port => 4001)
现在,当我运行服务器并执行类似http://0.0.0.0:4001/command=r&number=123123123的操作时,我总是得到重复的输出,例如:
“没有命令”“没有数字”“没有命令”“没有数字”
第一件事是为什么我会收到重复的请求? 与浏览器有关吗? 因为当我使用curl时,我没有相同的行为,其二,为什么我无法获得参数?
有关此类服务器最佳实现的任何技巧将不胜感激
提前致谢 。
第二个请求来自浏览器,查找favicon.ico
。 您可以通过在处理程序中添加以下代码来检查请求:
params = Rack::Request.new(env).params
p env # add this line to see the request in your console window
或者,您可以使用Sinatra :
require "rubygems"
require "amqp"
require "msgpack"
require "sinatra"
get '/:command/:number' do
command = params['command'].strip rescue "no command"
number = params['number'].strip rescue "no number"
p command
p number
AMQP.start do
if command =~ /\A(create|c|r|register)\z/i
MQ.queue("create").publish(number)
elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
nd
end
return command
end
然后在命令行中运行ruby the_server.rb
以启动http服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.