How do I add domains to Heroku app using Ruby code?
I'm creating an app that allows users to use their own domain. What method do I use in my Rails app to automatically register their chosen domain with Hero开发者_如何学Pythonku? I'll also need to deregister it if they change it.
I have contacted Heroku for the same thing, and they just pointed me at their api, and said it is fine to use it that way.
I'm afraid there isn't. Our API is "documented" only by the code of the client.
You may find our google group helpful for getting advice from community members as well: http://groups.google.com/group/heroku/
Oren
Here's the simple how-to:
require 'heroku'
heroku = Heroku::Client.new('heroku_username', 'heroku_password')
heroku.add_domain('heroku_app_name', 'example.com')
heroku.remove_domain('heroku_app_name','example.com')
See the api for more.
Of course I'd recommend against putting a plaintext password into your code. A nice thing you can do is use the heroku environment variables to get your passwords out of the code.
heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])
and then you can set the environment variables on your app with
$> heroku config:add HEROKU_USER='heroku_username'
$> heroku config:add HEROKU_PASSWORD='heroku_password'
from the command line.
The heroku gem has now been deprecated. You should use the heroku.rb gem instead. The commands have changed slightly, but it's essentially the same.
require 'heroku-api'
heroku = Heroku::API.new(:api_key => API_KEY) # use API Key
heroku = Heroku::API.new(:username => USERNAME, :password => PASSWORD) # use username and password
heroku = Heroku::API.new(:headers => {'User-Agent' => 'custom'}) # use custom header
heroku.delete_domain('app', 'example.com') # remove the 'example.com' domain from the 'app' app
heroku.get_domains('app') # list configured domains for the 'app' app
heroku.post_domain('app', 'example.com') # add 'example.com' domain to the 'app' app
The way you usually add domains in Heroku is using the Heroku API through the Heroku gem.
There's a command called heroku domains:add
you can invoke
$ heroku domains:add example.com
As I said before, the client calls the Heroku API. You can extract the Heroku Domain API information from the library and create a custom script that calls the Heroku API to add and remove a domain from your app.
Here's the client source code.
Note. Because you are "reverse engineering" and API which appears to be not documented, you should ask Heroku permission to do that, just to be sure you are not creating something against their TOS.
The gem is from heroku and is the recommended approach. I just contacted Heroku with the same question and this was there response:
You need to tell Heroku about the domain and where to route it as well. The CNAME tells DNS how to get the request to Heroku. Now you must tell Heroku which app to send it to. You do this by adding the domain to your app. In your case you would need to run "heroku domains:add my.domain.com" to your app. You can also do this programmatically from inside your application over our API. See the Heroku gem (http://github.com/heroku/heroku) for an example of how to connect and use the API.
精彩评论