Hello, if there is a better list to ask this question, please point me to it ... I have a -- possibly silly -- question on ControlTower/Rack. I should admit that I am kinda new to this whole thing.... My rack script has something like this: map '/foo' do $text = "foo" print $text run Proc.new {|env| [200, {"Content-Type" => "text/html"}, $text ] } end Now, it seems that this block is called immediately upon start, and the response is cached, i.e. it is not evaluated again when the resource /foo is actually read. What am I doing wrong? I want the block to be executed everytime that /foo is accessed. Thanks Alex
Hi Alex, This is as good a place as any for the time being. So, the way that Rack works is that it takes an app *object* (not a class) and calls the "call" method on that object each time a request comes in. To facilitate the use of middleware and the combination of multiple Rack "app objects" in one server process, Rack also comes with a builder which uses a minimal DSL to collect these objects. What you're using is that builder DSL. So, to break it down:
map '/foo' do # The 'map' keyword in the DSL tells Rack that any time a request that starts with "/foo" comes in, it should use the app contained inside this block (which Rack finds by #call'ing the block passed to 'map' at start up. $text = "foo" print $text # This is setup that will occur when Rack first #call's the block run Proc.new {|env| [200, {"Content-Type" => "text/html"}, $text ] } # The 'run' keyword tells Rack that the argument following 'run' is the "app object". In this case, you've provided a Proc object. Each HTTP request that comes in, with a path starting with "/foo", will #call this Proc object. Put your "print $text" line inside the block passed to Proc.new, and you'll see it getting called each time. end
If you're curious about the Rack::Builder DSL, the entire thing is only 55 lines. You can find it in the ControlTower project in lib/control_tower/vendor/rack/builder.rb Cheers, Josh
participants (2)
-
Alexander v. Below
-
Joshua Ballanco