How to get the HTTP_HOST from Rails? - ruby-on-rails

I need to set some server specific variables in a rails application that will run on at least two different servers. What is the best way to get the request's HTTP_HOST value in order to know what is the current server and set these variables accordingly?
I'm using Apache 2 with Passenger.

Think you're looking for request.env["SERVER_ADDR"].

Are the 2 servers involved in load balancing? If so, you won't want to grab the host from the request, because it'll be the same in both cases. You'll want to grab it in some generic ruby call. It's been a while since I've done Ruby, but probably something like (untested - rough idea):
Resolv::Hosts.getNames(Resolv::Hosts.getAddress('localhost'))

Related

Get Rails application domain name without request

Is there a way to get Rails` app domain name without use of the request?
I know how to get URL from the request, but what if there is no request and Rails is just running a delayed job task, can i get a domain name of the server where the Rails app is hosted?
There isn't a per se way of doing this due to how Rails is built but if it is a big enough project it might me stored somewhere.
So you could look for a place where it is being set and use that elsewhere.
For example, if the project uses request_store you could search for something like:
RequestStore.store[:host] = SomeTable.stored_host_name
And use SomeTable.stored_host_name in your worker or migration or the place you want to use it which doesn't have a request at hand.
Have a great day!

Partial migration from Rails to Phoenix

I have a rails app, and I want to gradually move it to Phoenix. While I implement functionality I want phoenix intercept the requests that are already implemented while passing unknown requests down to Rails app. What would be the best strategy in this case?
1) If I'm ready to accept some overhead, I could create a plug and route all unknown requests there (last route /*path). But how do I pass request intact and return the response? Parse it and then build again with HTTPoison would by unnecessary work, any better ideas?
2) I'm not sure, if it's possible with haproxy, but old app could be a fallback, where request would be passed if main backend responds with some error. Would this introduce less overhead?
3) Finally I could just split requests by mask in haproxy, but it seems like to much work, cause I'm planning on using rails for CUD actions and phoenix for R for some resources.
Any other options? Examples how someone done that? Thank you!
Read an excellent post about your exact problem here .
The basic idea is to use a gem rails-reverse-proxy to define a proxy to your Phoenix application.
Then, develop your feature on Phoenix, and add the necessary routes. Keep the rails conventions(it's the way the phoenix router works anyway).
Wire your rails app with a 'dummy' controller and set it to use rails-reverse-proxy.
It is recommended that you make all the ActiveRecord models that are owned by the Phoenix app read only. By adding a hook after_initialize :readonly! to the models owned by pheonix. This way you can use the models in Rails without compromising the phoenix ownership. Only the Phoenix app can change the model state.

How to do URL rewrite in Rails?

I have a rails app that is accessible through multiple URLs, I was wondering what is the best way to rewrite the URL to use the main domain name, abc.com.
I have a bunch of other domain names like
1kjsdf.info
2lksjdfs.info
3sldkjfds.info
... in total 50 of these kinds of domains.
They all end in info if that makes it easier. I used lighttpd as my webserver, is there a way to set things up so that when the user goes to 1kjsdf.info\profile, the url is rewritten as adc.com\profile?
You should probably do this in lighttpd, not Rails.
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
It will be much faster to service these requests in the HTTP server before letting it get to Rails.

Accessing URL/URI information in Rails in the environments file?

How would I access the domain name from Rails inside the environments/deveopment.rb? I want to find out whether or not I am on localhost or a real domain. Thank you!
The environment files are not processed on every request- only at startup, so there wouldn't be any particular context at the time they are processed. This is really the job for a separate environment instead of having dev do double duty.
If you just want to check against the hostname or something though, you can do something like (assuming you're on a unix-like system):
my_host = `hostname`.chomp
and test against that, which may solve your problem.

Concurrent Net::HTTP.get to same domain

As part of my Ruby on Rails application, I need to perform several (a few dozen) web requests to a foreign web server -- all on the same domain. I am aware of the two requests per domain throttle on Windows and know how to adjust that, but this application is running on CentOS and I was not expecting to run into this same issue, but I seem to be.
Does this same-domain-throttling exist on *nix? If so, how can I adjust it?
I think its not a OS issue but a Protocol issue.
Http 1.1 allows only 2 simulataneous requests to a hostname.
Note: http://one.yourdomain.com, http://two.yourdomain.com are considered different hostnames
HTH
I think it works better with other libraries like Typhoeus : Typhoeus or, if you're usning EventMachine : EM-Http-Request they both allow multi-request and you should be able to send more than 2 to the same domain (at least it does with Ubuntu)

Resources