简体   繁体   English

如何使用Ruby代码将域添加到Heroku应用程序?

[英]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 Heroku? 我在Rails应用程序中使用什么方法自动注册他们选择的域名? 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. 我已经联系了Heroku同样的事情,他们只是把我指向他们的api ,然后说这样就可以了。

I'm afraid there isn't. 我担心没有。 Our API is "documented" only by the code of the client. 我们的API仅由客户代码“记录”。

You may find our google group helpful for getting advice from community members as well: http://groups.google.com/group/heroku/ 您可能会发现我们的Google群组也有助于从社区成员那里获得建议: 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. 请参阅api了解更多信息。

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 = 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. heroku gem现已被弃用。 You should use the heroku.rb gem instead. 你应该使用heroku.rb gem。 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 . 您通常在Heroku中添加域的方式是通过Heroku gem使用Heroku API。 There's a command called heroku domains:add you can invoke 有一个名为heroku domains:add的命令heroku domains:add你可以调用

$ heroku domains:add example.com

As I said before, the client calls the Heroku API. 正如我之前所说,客户端调用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. 您可以从库中提取Heroku域API信息,并创建一个调用Heroku API的自定义脚本,以便从您的应用中添加和删除域。

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. 因为你是“逆向工程”和似乎没有记录的API,你应该要求Heroku这样做,只是为了确保你没有针对他们的服务条款创建一些东西。

The gem is from heroku and is the recommended approach. 宝石来自heroku,是推荐的方法。 I just contacted Heroku with the same question and this was there response: 我刚刚用同样的问题联系了Heroku,这是有回应的:

You need to tell Heroku about the domain and where to route it as well. 您需要告诉Heroku关于域以及路由它的位置。 The CNAME tells DNS how to get the request to Heroku. CNAME告诉DNS如何获取Heroku的请求。 Now you must tell Heroku which app to send it to. 现在你必须告诉Heroku将它发送到哪个应用程序。 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. 在您的情况下,您需要运行“heroku domains:add my.domain.com”到您的应用程序。 You can also do this programmatically from inside your application over our API. 您也可以通过我们的API从应用程序内部以编程方式执行此操作。 See the Heroku gem (http://github.com/heroku/heroku) for an example of how to connect and use the API. 有关如何连接和使用API​​的示例,请参阅Heroku gem(http://github.com/heroku/heroku)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM