1

I'm writing an OmniAuth dynamic provider and in the callback I need to get the current hostname (the app works in several different hostnames). I don't have the request object, just a big env hash. I found this hash contains an entry, env["SERVER_NAME"], with the hostname, but I'm not sure if that's a stable entry or it may change depending on the web server or stuff like that.

I also found the hostname buried in env["action_dispatch.routes"] in the member variable @host.

Any ideas what's the best or canonical way to get the hostname at this point?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

2 Answers2

3

The app is not in production yet, but so far the the private beta, using env["SERVER_NAME"] worked like a charm.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
1

In a rails controller, you can simply use:

request.host

Though I'm not sure if that sources anything different from what you already looked at. This works fine for me in a Rails 3.1 app I'm working on.

agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • I'm not a Rails controller, I'm implementing the setup method for an OmniAuth dynamic provider. – Pablo Fernandez Dec 16 '11 at 13:18
  • I haven't tried it myself, but I suggest trying to reference request.host in your code there and see if it works. I have to admit, I'm not sure which variables are created inside Rack for this process. – agmcleod Dec 16 '11 at 14:01
  • yeah, I tried that, it didn't work. For now I'm using env["SERVER_NAME"] but I haven't tested in production yet. – Pablo Fernandez Dec 21 '11 at 09:47