Skip to content

Commit 732c267

Browse files
committed
chore: move populate.go to utils
1 parent e58c424 commit 732c267

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

apps/question-service/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ The server will be available at http://localhost:8080.
6060
To reset and repopulate the database, run the following command:
6161

6262
```bash
63-
go run populate.go
63+
go run main.go -populate
6464
```

apps/question-service/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
67
"log"
78
"net/http"
89
"os"
910
"question-service/handlers"
11+
"question-service/utils"
1012
"time"
1113

1214
"cloud.google.com/go/firestore"
@@ -51,6 +53,14 @@ func main() {
5153

5254
service := &handlers.Service{Client: client}
5355

56+
// Check flags if should populate instead.
57+
shouldPopulate := flag.Bool("populate", false, "Populate database")
58+
flag.Parse()
59+
if *shouldPopulate {
60+
utils.Populate(client)
61+
return
62+
}
63+
5464
// Set up chi router
5565
r := chi.NewRouter()
5666
r.Use(middleware.Logger)

apps/question-service/populate.go renamed to apps/question-service/utils/populate.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
package main
1+
package utils
22

33
import (
4-
"cloud.google.com/go/firestore"
54
"context"
6-
firebase "firebase.google.com/go/v4"
7-
"fmt"
8-
"google.golang.org/api/iterator"
9-
"google.golang.org/api/option"
105
"log"
116
"question-service/models"
127
"time"
8+
9+
"cloud.google.com/go/firestore"
10+
"google.golang.org/api/iterator"
1311
)
1412

1513
// PopulateSampleQuestionsInTransaction deletes all existing questions and then adds new ones in a single transaction
@@ -295,32 +293,11 @@ Return the result table in any order.`,
295293
})
296294
}
297295

298-
// initFirestore initializes the Firestore client
299-
func initFirestore(ctx context.Context, credentialsPath string) (*firestore.Client, error) {
300-
opt := option.WithCredentialsFile(credentialsPath)
301-
app, err := firebase.NewApp(ctx, nil, opt)
302-
if err != nil {
303-
return nil, fmt.Errorf("failed to initialize Firebase App: %v", err)
304-
}
305-
306-
client, err := app.Firestore(ctx)
307-
if err != nil {
308-
return nil, fmt.Errorf("failed to get Firestore client: %v", err)
309-
}
310-
return client, nil
311-
}
312-
313-
func main() {
314-
// Initialize Firestore client
296+
func Populate(client *firestore.Client) {
315297
ctx := context.Background()
316-
client, err := initFirestore(ctx, "cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json")
317-
if err != nil {
318-
log.Fatalf("Failed to initialize Firestore client: %v", err)
319-
}
320-
defer client.Close()
321298

322299
// Run the transaction to delete all questions and add new ones
323-
err = populateSampleQuestionsInTransaction(ctx, client)
300+
err := populateSampleQuestionsInTransaction(ctx, client)
324301
if err != nil {
325302
log.Fatalf("Failed to populate sample questions in transaction: %v", err)
326303
}

0 commit comments

Comments
 (0)