2016-07-23 50 views
0

我是Heroku的新手。你如何在Heroku中隐藏你的API密钥?我知道从文档我可以使用配置增值服务使用Rails在Heroku中隐藏Google API密钥

https://devcenter.heroku.com/articles/config-vars 

但是,我如何从JavaScript加载配置? 我想要做的就是这样的事情

https://developers.google.com/maps/documentation/javascript/tutorial 

我使用Ruby on Rails的我的应用程序。

+3

[Rails的4.1推秘密的Heroku(HTTP的可能重复:// stackoverflow.com/questions/21894761/rails-4-1-pushing-secrets-to-heroku) –

回答

2

使用API​​密钥而不将它们提交到代码中的一个好方法是使用环境变量。这些变量是在用于为代码中可引用的Rails应用程序使用的系统的shell中设置的。在Heroku上,您将设置一个环境变量,而不是在标准Linux服务器上。

首先,在你的Rails应用程序,你会插入一个环境变量这样的值:ENV["MY_API_KEY"]

棒,凡是你需要把你的API密钥的Rails应用程序,然后设置环境变量在此命令的Heroku API密钥:heroku config:set MY_API_KEY=some_long_example_api_key_09823098270098

现在,在您的Rails应用程序时,您使用ENV["MY_API_KEY"]将在地方插入时,您的应用程序在Heroku上运行值some_long_example_api_key_09823098270098

如果你想一个.js.erb文件中引用的环境变量,你就需要把它写成<%= ENV["MY_API_KEY"] %>

Heroku documentation