我将在 Google App-Engine 中托管我的 RoR API-App。
到目前为止一切正常,但我必须在 app.yaml
中以纯文本形式存储用户名、密码和 key (例如数据库用户/密码)。这太愚蠢了,所以我永远无法将它推送到我的 git 仓库!通常我将此类内容存储在环境变量中并在我的应用程序中使用它们。
但我没有找到设置或访问环境变量的方法。
有没有办法或替代方法来做到这一点?
最佳答案
我做到了!
对于本地开发,我只是像往常一样设置我的环境。
如果模式是 Production,我从 Google Datastore 加载它们所有键值对并将它们设置为环境变量。
我在 Initializer 中执行此操作,只需在 YourApp/config/initializers/
中创建一个文件并将代码放入其中!只需创建新实体,复制代码中的 Kind 名称并设置您的项目 ID。由于您的应用托管在 Google 中,因此它应该可以访问数据存储(您需要在 IAM-Manager 中设置权限)
require "google/cloud/datastore"
# Load the enviroment variables from the google datastore!
if Rails.env == "production"
data_store = Google::Cloud::Datastore.new(
project_id: 'YOUR_PROJECT_ID'
)
query = data_store.query "YOUR_KIND_NAME"
results = data_store.run query
puts "Set custom env variables!"
# Set each result as an env variable
results[0].properties.to_h.each do |key, value|
ENV[key]= value
end
end
关于ruby-on-rails - 在 Google Cloud App Engine 中托管 Rails App 并设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395592/