如何使用基于heroku应用程序名称的谷歌API键 [英] how to use google api keys based on heroku application name

查看:83
本文介绍了如何使用基于heroku应用程序名称的谷歌API键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为在heroku上托管的应用创建了几个不同的环境,因此我拥有:
appName-staging.heroku.com
appName-production.heroku.com



我想为这些应用程序使用不同的Google API密钥,我该怎么做?
我创建了一个如下所示的google.yml文件:



开发:
api_key:'ABCXYZ'



制作:
api_key:'DEFXYZ'



所以我在本地开发时使用ABCSZY,而appName- production.heroku使用DEFXYZ .com
的问题是,我如何让appName-staging.heroku.com使用不同的密钥?因为每个部署到Heroku的应用程序都被认为是生产,所以
appName-staging.heroku.com和appName-production.heroku.com使用相同的密钥。

解决方案

您可以添加 heroku配置变量给每个环境,允许你从应用程序中识别每一个。





  $ heroku config:添加APP_NAME_ENV =生产--app appName生产
$ heroku配置:添加APP_NAME_ENV = staging --app appName -staging

然后您可以使用以下命令从应用程序中获取当前环境:

  ENV ['APP_NAME_ENV'] 

如果你的YAML文件是一个叫做 GOOGLE_KEYS 的哈希文件,那么下面的代码将返回给定环境的正确密钥:

  GOOGLE_KEYS [ENV ['APP_NAME_ENV']] 


I've created a few different "environments" for my app that is hosted on heroku so I have: appName-staging.heroku.com appName-production.heroku.com

I want to use different google api keys for these applications, how do I do this? i've created a google.yml file that looks like:

development: api_key: 'ABCXYZ'

production: api_key: 'DEFXYZ'

so I use ABCSZY when developing locally, and DEFXYZ for appName-production.heroku.com question is, how do i get appName-staging.heroku.com to use a different key?

since every application deployed to Heroku is considered to be in "production", both appName-staging.heroku.com and appName-production.heroku.com use the same key.

解决方案

You could add a heroku config variable to each environment, allowing you to identify each one from within the app.

Something along the lines of:

$ heroku config:add APP_NAME_ENV=production --app appName-production
$ heroku config:add APP_NAME_ENV=staging --app appName-staging

Then you could grab the current environment from within your app using:

ENV['APP_NAME_ENV']

And if you've got your YAML file as a hash called something like GOOGLE_KEYS, the following would return the correct key for a given environment:

GOOGLE_KEYS[ENV['APP_NAME_ENV']]

这篇关于如何使用基于heroku应用程序名称的谷歌API键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆