Sinatra - how do I get the server's domain name

Louis Sayers picture Louis Sayers · May 16, 2011 · Viewed 12.2k times · Source

I'm trying to get the domain name in my Sinatra app but as a newbie I really am struggling to figure out how to do this, and I know it must be possible!

Rack::Request#host_with_port looks promising, but I don't know how to get this from my app - how do I get stuff from Rack in my Ruby code?

Or is there another way - I'm thinking I don't really want to do this every time a request happens (although it's not too bad), but I thought it'd be better if I could just do it once when the application loads up.

Any hints?

Answer

intellidiot picture intellidiot · May 16, 2011

simply use request.host inside your code.

get  "/" do
  puts request.host #=> localhost
end