small medium large xlarge

Generic-user-small
31 Jan 2009, 12:40
James Frost (3 posts)

Hiya,

I loved the screencast, and Sinatra looks fantastic. However, I had a slight issue once I’d deployed my app to my server. The hashed URL it gives me is like:

http://0.0.0.0:4567/2062a677e4dfe43fae94323ae1bdc79284da43ea

Which obviously, isn’t correct - would you have any idea why this uses that IP and the development port number? I’m serving the app up with passenger now, with a proper domain name. Is there a configuration option I’ve missed somewhere? Or something missing in the code?

Any help would be greatly appreciated! :)

Thanks!

Generic-user-small
31 Jan 2009, 13:08
James Frost (3 posts)

Just did a little more investigating, and it seems like

Sinatra::Application.host is returning 0.0.0.0 and Sinatra::Application.port is returning 4567, even when deployed.

I can’t work out why!

2223901045_12c5c7118b_s_pragsmall
01 Feb 2009, 23:26
Adam Keys (4 posts)

Nice find, James.

Sinatra is falling back to its defaults here. So, you could pass options to your Sinatra app to override @Sinatra::Application.host@ or @Sinatra::Application.port@. Or, you could bypass @base_url@ and send only relative URLs. The one place you’d want to use an absolute URL is in your handler that redirects the user to the destination site. I’d favor the latter approach.

Make sense?

Generic-user-small
02 Feb 2009, 15:37
James Frost (3 posts)

Thanks for that, Adam. Your suggestions do make sense, but how would you do the latter and have the contents of the textarea on the info page of a URL contain the correct URL?

2223901045_12c5c7118b_s_pragsmall
03 Feb 2009, 14:52
Adam Keys (4 posts)

Good point, James. Rack’s request object provides a @host@ method like this:

    def host
      # Remove port number.
      (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
    end

Depending on how you’re running your app (Passsenger, Mongrel, Thin, etc.), that may give you the info you need to generate an absolute URL.

You could also set an environment variable for your server. You’d want to do this in the command that starts it if you’re using Mongrel, Thin, etc. or in your Apache config if you’re using Passenger.

Finally, you could set a constant in your configure block. Like so:

configures :production do
  HOST = 'myapp.com'
  PORT = 80
end
You must be logged in to comment