Matthias Hryniszak
Matthias Hryniszak

Reputation: 3162

Getting Rack mounted path in Sinatra application

Suppose I have the following config.ru file

require './status.rb'

map "/status" do
  run Sinatra::Application
end

and the status.rb is a simple

require 'sinatra'

get '/' do
  'Some status here...'
end

I'd like to know where the Sinatra application is mounted inside status.rb (for example to provide proper paths to resources). Is there a way of retrieving that information from Rack?

Upvotes: 2

Views: 680

Answers (1)

matt
matt

Reputation: 79723

To get where the app is mounted you can use request.script_name.

get '/' do
  p request.script_name # will print "/status"
  'Some status here...'
end

If you’re generating urls for resources, you might want to look at the url method instead. That will take into account things like proxies as well as where the app is mounted:

get '/' do
  p url('foo') # will print "http://localhost:9292/status/foo"
  'Some status here...'
end

Upvotes: 3

Related Questions