How to set variables based on project id?

2019-07-23 02:59发布

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

2条回答
相关推荐>>
2楼-- · 2019-07-23 03:18

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
查看更多
萌系小妹纸
3楼-- · 2019-07-23 03:26

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
}
查看更多
登录 后发表回答