Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIREBASE_DATABASE_EMULATOR_HOST environment variable with IP address errors and exits application #574

Open
Zakpak0 opened this issue Jul 31, 2023 · 1 comment

Comments

@Zakpak0
Copy link

Zakpak0 commented Jul 31, 2023

[REQUIRED] Step 2: Describe your environment

  • Operating System version: Windows 11 Pro
  • Firebase SDK version: firebase.google.com/go/v4
  • Library version: _____
  • Firebase Product: database

[REQUIRED] Step 3: Describe the problem

Setting the FIREBASE_DATABASE_EMULATOR_HOST environment variable with an IP address errors and exit's the application.

Steps to reproduce:

Create an application using the firebase sdk for golang
Load FIREBASE_DATABASE_EMULATOR_HOST={IP_ADDRESS} into your environment
The application should crash
image

Relevant Code:

package firebase

import (
	"context"
	"log"
	"os"

	fb "firebase.google.com/go/v4"
	"google.golang.org/api/option"
)

func Firebase(ctx context.Context) *fb.App {
	db := os.Getenv("DATABASE_URL")
	if db == "" {
		log.Fatal("Error loading DATABASE_URL from .env file")
	}
	pid := os.Getenv("PROJECT_ID")
	if pid == "" {
		log.Fatal("Error loading PROJECT_ID from .env file")
	}
	config := &fb.Config{
		DatabaseURL: db,
		ProjectID:   pid,
	}
	env := os.Getenv("APP_ENV")
	var opt option.ClientOption
	if env == "development" {
		cred := os.Getenv("FIREBASE_CONFIG")
		if cred == "" {
			log.Fatal("Error loading FIREBASE_CONFIG from .env file")
		}
		opt = option.WithCredentialsFile(cred)
	}
	if value := os.Getenv("FIRESTORE_EMULATOR_HOST"); value != "" {
		log.Printf("Using Firestore Emulator: %s", value)
		app, err := fb.NewApp(ctx, config, opt)
		if err != nil {
			log.Fatalln(err)
		}
		return app
	} else {
		app, err := fb.NewApp(ctx, config)
		if err != nil {
			log.Fatalln(err)
		}
		return app
	}
}
@JustinTrvz
Copy link

JustinTrvz commented Oct 16, 2023

config := &firebase.Config{
       ProjectID:     "<project-id>",
       DatabaseURL:   "localhost:9000/?ns=<project-id>",
}

You can try this as a workaround as long as the fix is not implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants