如何根据项目ID设置变量?

huangapple go评论65阅读模式
英文:

How to set variables based on project id?

问题

我目前有一个使用Go语言开发的App Engine应用,有两个项目:myapp-prodmyapp-staging

我想根据应用程序运行的环境来设置某些变量的值。

应用程序是否能够检测到它正在运行的环境?谢谢。

英文:

I currently have an App Engine Go app with 2 projects: myapp-prod and myapp-staging.

I'd like to be able to set the value of certain variables depending if the app is running in prod or staging.

Is there a way for the app to detect which environment it is running in?

Thanks

答案1

得分: 2

你可以使用appengine.AppID()函数来获取你的应用程序的名称/ID:

// AppID返回当前应用程序的应用程序ID。
// 字符串将是一个纯应用程序ID(例如“appid”),对于自定义域部署,它将带有域前缀(例如“example.com:appid”)。
func AppID(c Context) string

你可以使用appengine.IsDevAppServer()来判断你的应用程序是在开发模式(使用AppEngine SDK)还是在生产环境中运行:

// IsDevAppServer报告App Engine应用程序是否在开发App Server中运行。
func IsDevAppServer() bool

或者你也可以使用appengine.ServerSoftware(),它包含上述两个信息,合并为一个字符串:

// ServerSoftware返回App Engine发布版本。
// 在生产环境中,它看起来像“Google App Engine/X.Y.Z”。
// 在开发应用服务器中,它看起来像“Development/X.Y”。
func ServerSoftware() string
英文:

You can use the appengine.AppID() function to get the name/id of your application:

// AppID returns the application ID for the current application.
// The string will be a plain application ID (e.g. "appid"), with a
// domain prefix for custom domain deployments (e.g. "example.com:appid"). 
func AppID(c Context) string

And you can use appengine.IsDevAppServer() to tell if your app is running in development mode (using the AppEngine SDK) or live (in production):

// IsDevAppServer reports whether the App Engine app is running in the
// development App Server. 
func IsDevAppServer() bool

Alternatively you can also use appengine.ServerSoftware() which contains both of the information above, merged into one string:

// ServerSoftware returns the App Engine release version.
// In production, it looks like "Google App Engine/X.Y.Z".
// In the development appserver, it looks like "Development/X.Y". 
func ServerSoftware() string

答案2

得分: 1

使用一个描述应用程序是处于生产环境还是测试环境的环境变量。在app.yml中添加以下内容:

env_variables:
  ENVIRONMENT: 'production'

在你的代码中:

import "os"

if v := os.Getenv("ENVIRONMENT"); v == "production" {
  // 你处于生产环境中
}
英文:

Use an environment variable describing whether your app is on production or staging. Add to app.yml,

env_variables:
  ENVIRONMENT: 'production'

In your code,

import "os"

if v := os.Getenv("ENVIRONMENT"); v == "production" {
  // You're in production
}

huangapple
  • 本文由 发表于 2016年3月24日 01:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/36184603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定