Louis Sayers Louis Sayers - 5 days ago 6
Ruby Question

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

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

simply use request.host inside your code.

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