Ruby cgi needs to reload apache for new value?

微笑、不失礼 提交于 2020-01-06 18:03:24

问题


I have phusion-passenger installed with apache on Ubuntu. In my config.ru, I have the following code:

require 'cgi'

$tpl = CGI.new['myvar'] + '.rb'

app = proc do |env|
    [200, { "Content-Type" => "text/html" }, [$tpl]]
end
run app

So then when I go to my browser at http://localhost/?myvar=hello, I see the word hello printed out, which is fine. Then I change the url to http://localhost/?myvar=world, but the page still shows hello. Only after I reload apache will the page show world.

Before using phusion-passenger, I was using mod_ruby with apache. If I remember correctly, I didn't need to restart apache to get the CGI variable to print the updated value.

I'm not stuck on needing to use CGI. I just want to be able to grab query string parameters without having to reload apache each time.

I'm not using rails or Sinatra because i'm just trying to wrap my head around the Ruby language and what phusion-passenger with apache is all about.


回答1:


IMO this behavior makes sense. Because $tpl is set only once when the file is loaded, what happens when the first request is served. After that - in following requests - only the proc is called, but that does not change $tpl anymore.

Instead of using plain CGI, I would do it with a very simple Rack app:

require 'rack'
require 'rack/server'

class Server
  def self.call(env)
    req = Rack::Request.new(env)
    tpl = "#{req.params['myvar']}.rb"
    [200, {}, [tpl]]
  end
end

run Server


来源:https://stackoverflow.com/questions/26412341/ruby-cgi-needs-to-reload-apache-for-new-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!