Thin HTTP 服务器下的 Ruby AMQP

发布于 2024-12-12 14:13:50 字数 1035 浏览 6 评论 0原文

我正在运行一个简单的瘦服务器,它将一些消息发布到不同的队列,代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-19 14:13:51

第二个请求来自寻找 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

然后运行 ​​< 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:

 params  = Rack::Request.new(env).params
 p env # add this line to see the request in your console window

Alternatively you could use 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

and then run ruby the_server.rb at the command line to start the http server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文