Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 293
Louis Sayers Avatar asked May 16 '11 11:05

Louis Sayers


People also ask

When should I take Sinatra?

Sinatra is powerful enough to develop a functioning web application with just a single file. Sinatra is recognized to be a good way for novice developers to get started in web application development in Ruby and can help prepare in learning for larger frameworks, including Rails.

What is Sinatra framework?

Sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Merb, Nitro, and Camping. It is dependent on the Rack web server interface. It is named after musician Frank Sinatra.


2 Answers

simply use request.host inside your code.

get  "/" do   puts request.host #=> localhost end 
like image 174
intellidiot Avatar answered Oct 14 '22 00:10

intellidiot


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"] 
like image 35
makevoid Avatar answered Oct 13 '22 23:10

makevoid