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.") }
CodePudding user response:
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.