如何区分Google AppEngine上的本地部署和实时部署 [英] How to differentiate between local deployment and live deployment on Google AppEngine

查看:157
本文介绍了如何区分Google AppEngine上的本地部署和实时部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置全局变量,例如:

I want to set global variable, for example:

var DEBUG_MODE bool

func init() {
  // set DEBUG_MODE true if localhost (not appspot.com or other domain)
}


,则返回true

如何做到这一点?

How to do this?

推荐答案

最简单的就是使用use appengine.IsDevAppServer() 来判断您的app正在开发模式下运行(使用AppEngine SDK)或直播(制作中):

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


func IsDevAppServer() bool

IsDevAppServer报告App Engine应用程序是否在开发App Server中运行。 / p>

IsDevAppServer reports whether the App Engine app is running in the development App Server.

或者,您也可以使用 appengine.ServerSoftware() 其中包含这些信息以及您的应用版本,合并为一个字符串:

Alternatively you can also use appengine.ServerSoftware() which contains this information along with your App version, merged into one string:


func ServerSoftware() string

ServerSoftware返回App Engine发行版本。在制作中,它看起来像Google App Engine / X.Y.Z。在开发应用程序服务器中,它看起来像Development / XY。

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".

例如,你想要做什么:

var DEBUG_MODE bool

func init() {
    DEBUG_MODE = appengine.IsDevAppServer()
}

或在一行内:

Or in one line:

var DEBUG_MODE = appengine.IsDevAppServer()

尽管注意到只要您引用 DEBUG_MODE ,就可以调用 appengine.IsDevAppServer()。另外,名称 DEBUG_MODE 不符合Go命名约定,如果需要导出它,它应该是 DebugMode 因为你想从其他包中访问它),或者如果它不需要导出,它应该是 debugMode

Although note that you could just call appengine.IsDevAppServer() whenever you would refer to DEBUG_MODE. Also the name DEBUG_MODE does not conform to Go naming conventions, it should either be DebugMode if it needs to be exported (because you want to access it from other packages too), or it should be debugMode if it doesn't need to be exported.

看到这个相关的问题(可能重复?):如何根据项目ID设置变量?

See this related question (possible duplicate?): How to set variables based on project id?

这篇关于如何区分Google AppEngine上的本地部署和实时部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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