Rack: How do you store the URL as a variable?
You shouldn't need the map part.
run Proc.new { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=6400'
},
File.open( 'archive' + env['PATH_INFO'], File::RDONLY)
]
}
Get the current page URL in Ruby/Rack
Since Serve uses Rack under the hood, try this in a view:
<%= request.url %>
How to setup URLs for static site with Ruby Rack on Heroku
For now I found the best answer to be:
use Rack::Static,
:urls => ["/media/images", "/media/js", "/media/css"],
:root => "public"
map "/" do
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
end
map "/portfolio" do
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/portfolio/index.html', File::RDONLY)
]
}
end
And map every URL to its respective file. Tedious, but works. See also the answer to this question regarding URL variables. Couldn't get it to work for me though. How to generate URL with host in Rails development environment?
You could use an environment variable to configure a default host. See Luc Boissaye's answer on how to set this up with dotenv
. If you don't add the .env
to your repo, each developer can create his own .env
and configure his (or her) own preferences.
But you can also use existing environment variables to configure the default_url_options
. For example Pow.cx sets the POW_DOMAINS
variable:
config.action_mailer.default_url_options = {
ENV['POW_DOMAINS'].present? ? 'my-app.dev' : 'localhost'
}
As far as I know you only need to set the config.action_controller.default_url_options
if you want to force it to some default. When you use the rails _path
helpers in your views, this will generate a relative URL (/path
). If you use the _url
helpers, this will generate an absolute URL (http://your.host/path
), but both the protocol (http
) and host (your.host
) are request based. How do I access the Rack environment from within Rails?
I'm pretty sure you can use the Rack::Request
object for passing request-scope variables:
# middleware:
def call(env)
request = Rack::Request.new(env) # no matter how many times you do 'new' you always get the same object
request[:foo] = 'bar'
@app.call(env)
end
# Controller:
def index
if params[:foo] == 'bar'
...
end
end
Alternatively, you can get at that "env
" object directly:# middleware:
def call(env)
env['foo'] = 'bar'
@app.call(env)
end
# controller:
def index
if request.env['foo'] == 'bar'
...
end
end
What is the env variable in Rack middleware?
env
is just a hash. Rack itself and various middlewares add values into it.
To understand what the various keys are in the hash, check out the Rack Specification.
And here is a sample env hash:
{
"GATEWAY_INTERFACE" => "CGI/1.1",
"PATH_INFO" => "/index.html",
"QUERY_STRING" => "",
"REMOTE_ADDR" => "::1",
"REMOTE_HOST" => "localhost",
"REQUEST_METHOD" => "GET",
"REQUEST_URI" => "http://localhost:3000/index.html",
"SCRIPT_NAME" => "",
"SERVER_NAME" => "localhost",
"SERVER_PORT" => "3000",
"SERVER_PROTOCOL" => "HTTP/1.1",
"SERVER_SOFTWARE" => "WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)",
"HTTP_HOST" => "localhost:3000",
"HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0",
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"HTTP_ACCEPT_LANGUAGE" => "zh-tw,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"HTTP_ACCEPT_ENCODING" => "gzip, deflate",
"HTTP_COOKIE" => "jsonrpc.session=3iqp3ydRwFyqjcfO0GT2bzUh.bacc2786c7a81df0d0e950bec8fa1a9b1ba0bb61",
"HTTP_CONNECTION" => "keep-alive",
"HTTP_CACHE_CONTROL" => "max-age=0",
"rack.version" => [1, 2],
"rack.input" => #<StringIO:0x007fa1bce039f8>,
"rack.errors" => #<IO:<STDERR>>,
"rack.multithread" => true,
"rack.multiprocess" => false,
"rack.run_once" => false,
"rack.url_scheme" => "http",
"HTTP_VERSION" => "HTTP/1.1",
"REQUEST_PATH" => "/index.html"
}
And to make it easier to use, checkout Rack::Request which makes it easier to access the values inside the env hash. How can I route a url to a CGI script in Rack?
You can use the backticks to run the command and grab the output.
map "/check" do
run Proc.new { |env| [200, {"Content-Type" => "text/html"}, [`./httpd/cgi-bin/check_wrapper.sh`] ] }
end
run Rack::Directory.new("htdocs")
How do I set/get session vars in a Rack app?
session
is a method that is part of some web frameworks, for example Sinatra and Rails both have session
methods. Plain rack
applications don’t have a session
method, unless you add one yourself.
The session hash is stored in the rack env hash under the key rack.session
, so you can access it like this (assuming you’ve named the rack environment to your app env
):
env['rack.session'][:msg]="Hello Rack"
Alternatively, you could use Rack’s built in request
object, like this:request = Rack::Request.new(env)
request.session[:msg]="Hello Rack"
How best can I define ENV Variables in a Rack based app?
Apparently, the only problem is that I am not loading the .env.rb
file inside the spec helper directory. So solving it for me was to require .env.rb
file in my spec_helper.rb
and it worked.
spec/spec_helper.rb
require_relative '../../.env.rb'
Related Topics
Issue Installing Gems on Windows 7 with Proxy
How to Use Strong Parameters with an Objects Array in Rails
Regex to Check Alphanumeric String in Ruby
How to Use Omniauth to Make Authenticated Calls to Services
How to Reverse a Linked List in Ruby
Statically Compile Pdftk for Heroku. Need to Split PDF into Single Page Files
How to Generate The Form for an Existing Model in Rails
How to Calculate a String's Width in Ruby
Download a Carrierwave Upload from S3
How to Send a Keep-Alive Packet Through Websocket in Ruby on Rails
Ruby on Rails Private Link Sharing: Google Docs Style
Ubuntu 10 Ruby 1.9 Rails 3: No Such File or Directory
Use a String to Access a Local Variable by Name
Rails: What's Wrong with This Multiple Join with Conditions on The Associations
Actionmailer Pass Local Variables to The Erb Template