From 6892e77d358acc4f97e62db48e56a5a0a9081f59 Mon Sep 17 00:00:00 2001 From: PandaJW <5168pandayoyo@gmail.com> Date: Sat, 1 Jun 2024 21:18:59 +0800 Subject: [PATCH] Added databse url and hid env --- .gitignore | 4 +++- database.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0f5eb8b..d35e446 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ # certs and keys *.crt *.key -*.pem \ No newline at end of file +*.pem +edusync-firebase.json +.env \ No newline at end of file diff --git a/database.go b/database.go index 4c4afd9..77cfac7 100644 --- a/database.go +++ b/database.go @@ -3,36 +3,70 @@ package main import ( "context" "fmt" + + "log" "os" + "github.com/joho/godotenv" + firebase "firebase.google.com/go" "firebase.google.com/go/db" "google.golang.org/api/option" ) +// use godot package to load/read the .env file and +// return the value of the key +func goDotEnvVariable(key string) string { + + // load .env file + err := godotenv.Load(".env") + + if err != nil { + log.Fatalf("Error loading .env file") + } + + return os.Getenv(key) +} + type FireDB struct { *db.Client } var fireDB FireDB +// Connect initializes and connects to the Firebase Realtime Database. +// It uses the provided JSON file containing the service account key to authenticate with Firebase. +// The DatabaseURL is set to the Firebase project's Realtime Database URL. +// The function returns an error if any step fails during the initialization or connection process. func (db *FireDB) Connect() error { // Find home directory. home, err := os.Getwd() if err != nil { return err } + + // Create a context for the Firebase app. ctx := context.Background() - opt := option.WithCredentialsFile(home + "Your JSON File Location") - config := &firebase.Config{DatabaseURL: "Your Database URL"} + + // Set up the Firebase app with the provided JSON file containing the service account key. + opt := option.WithCredentialsFile(home + "edusync-firebase.json") + dotenv := goDotEnvVariable("DATABASE_URL") + config := &firebase.Config{DatabaseURL: dotenv} + app, err := firebase.NewApp(ctx, config, opt) + // opt := option.WithCredentialsFile("edusync-firebase.json") + // app, err := firebase.NewApp(context.Background(), nil, opt) if err != nil { return fmt.Errorf("error initializing app: %v", err) } + + // Initialize the Firebase Realtime Database client. client, err := app.Database(ctx) if err != nil { return fmt.Errorf("error initializing database: %v", err) } + + // Assign the Firebase Realtime Database client to the FireDB struct. db.Client = client return nil } @@ -48,8 +82,13 @@ func connectToFirebase() error { return err } - ref := fireDB.NewRef("/path/to/data") - err = ref.Set(context.Background(), "some value") + ref := fireDB.NewRef("/") + err = ref.Set(context.Background(), map[string]string{ + "name": "Jane Doe", + "age": "7", + "class": "Tech Explorer", + "instructor": "Scott Smith", + }) if err != nil { return err }