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

2019-04-03 05:49发布

问题:

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?

回答1:

simply use request.host inside your code.

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


回答2:

Take a look at:

request.env.inspect

so you can see all the request environment variables.

I think that you are looking for

request.env["SERVER_NAME"]