Thin HTTP 服务器下的 Ruby AMQP
我正在运行一个简单的瘦服务器,它将一些消息发布到不同的队列,代码如下所示:
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时我没有相同的行为,第二件事为什么我无法获取参数?
任何有关此类服务器的最佳实施的提示将不胜感激
。提前致谢。
I'm running a simple thin server, that publish some messages to different queues, the code looks like :
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)
Now when I run the server, and do something like http://0.0.0.0:4001/command=r&number=123123123
I'm always getting duplicate outputs, something like :
"no command"
"no number"
"no command"
"no number"
The first thing is why I'm getting like duplicate requests ? is it something has to do with the browser ? since when I use curl I'm not having the same behavior , and the second thing why I can't get the params ?
Any tips about the best implementation for such a server would be highly appreciated
Thanks in advance .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个请求来自寻找
favicon.ico
的浏览器。您可以通过在处理程序中添加以下代码来检查请求:或者您可以使用 Sinatra:
然后运行 < code>ruby the_server.rb 在命令行启动http服务器。
The second request comes from the browser looking for the
favicon.ico
. You can inspect the requests by adding the following code in your handler:Alternatively you could use Sinatra:
and then run
ruby the_server.rb
at the command line to start the http server.