我的GAE本地服务器如何连接到Firebase模拟器?

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

How does my GAE local server connect to firebase emulator

问题

我正在寻找技术上的理解,当我运行我的Go服务器go run main.go时,我能够连接到我的本地Firestore模拟器(假设模拟器正在运行)。

这个连接是如何建立的?

我之前一直认为这是理所当然的,但昨天它不起作用了,声称找不到我的GAE凭据。

我运行了:gcloud auth application-default login,然后运行服务器,发现我正在将信息写入我的生产数据库而不是模拟器...

编辑:谢谢Cerise的回答。

我忘了提到我的电脑重新启动了,我丢失了FIREBASE_EMULATOR_HOST环境变量的设置(导出)。因此,它默认为生产端点...我不知道,我只是“登录”到我的Google身份验证SDK...这导致凭据被用于生产数据库...重新设置变量后,我连接到了模拟器。

如果其他人也遇到了这个问题,我最终在我的代码中添加了一个简单的检查(我已经区分了我是在本地机器上运行还是在我的GAE服务器上运行):

if os.Getenv("FIRESTORE_EMULATOR_HOST") == "" { log.Fatalf("FIRESTORE_EMULATOR_HOST未设置,并尝试以开发模式运行。") }

英文:

I'm looking to understand technically how when I run my go server go run main.go
I'm able to connect to my local firestore emulator (assuming the emulator is running)

How exactly is that connection being established?

Asking because I've always taken it for granted and yesterday it wasn't working claiming that my GAE credentials could not be found.

I ran: gcloud auth application-default login and after I ran my server, I found out that I was writing information to my PRODUCTION db instead....

EDIT: Thanks Cerise for answer.

I forgot to mention that my computer restarted and I had lost the setting (exporting) of the FIREBASE_EMULATOR_HOST environment.. Because of this, it was defaulting to the prod endpoint.. I didn't know and I just "logged in" to my google auth sdk.. which led the credentials to be used to the prod database.. After setting the variable again, I connected to the emulator.

If this has happened to others, I ended up adding a simple check in my code (where I already differentiate if I'm running in my local machine or in my GAE server)

if os.Getenv("FIRESTORE_EMULATOR_HOST") == "" { log.Fatalf("FIRESTORE_EMULATOR_HOST not set and attempting to run in dev mode.") }

答案1

得分: 1

Go Firestore客户端使用由FIRESTORE_EMULATOR_HOST环境变量指定的模拟器。

使用gcloud beta emulators firestore start命令启动模拟器。模拟器会打印设置FIRESTORE_EMULATOR_HOST环境变量的说明。

检查环境变量以确定客户端是否在使用模拟器:

usingEmulator := os.Getenv("FIRESTORE_EMULATOR_HOST") != ""

有关更多信息,请参阅包文档

英文:

The Go Firestore client uses the emulator specified by the FIRESTORE_EMULATOR_HOST environment variable.

Use the gcloud beta emulators firestore start command to start the emulator. The emulator prints instructions for setting the FIRESTORE_EMULATOR_HOST environment variable.

Check the environment variable to determine if the client is using the emulator:

usingEmulator := os.Getenv("FIRESTORE_EMULATOR_HOST") != “”

See the package documentation for more information.

huangapple
  • 本文由 发表于 2021年9月27日 03:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/69338359.html
匿名

发表评论

匿名网友

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

确定