Skip to content

Commit

Permalink
fix: use firestore admin sdk (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Jan 14, 2024
1 parent 80d9fc7 commit 4f9f4d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ steps:
"--image",
"${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPOSITORY}/${_SERVICE_NAME}:${TAG_NAME}",
"--set-env-vars",
"ENVIRONMENT=${_ENVIRONMENT},ALLOW_ORIGIN=${_ALLOW_ORIGIN},TRUSTED_PROXY=${_TRUSTED_PROXY},FIREBASE_PROJECT_ID=${PROJECT_ID}",
"ENVIRONMENT=${_ENVIRONMENT},ALLOW_ORIGIN=${_ALLOW_ORIGIN},TRUSTED_PROXY=${_TRUSTED_PROXY},GOOGLE_CLOUD_PROJECT_ID=${PROJECT_ID}",
"--region",
"${_REGION}",
"--service-account",
Expand Down
12 changes: 10 additions & 2 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
gotenv.Load(fmt.Sprintf(".env.%v", mode))
gotenv.Load(fmt.Sprintf(".env.%v.local", mode))

err := db.InitDatabaseClient(os.Getenv("FIREBASE_PROJECT_ID"))
err := db.InitDatabaseClient(os.Getenv("GOOGLE_CLOUD_PROJECT_ID"))
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
Expand All @@ -48,5 +48,13 @@ func main() {

router.POST("/api/hello-world", api.HelloWorldHandler)

router.Run(":8080")
err = router.Run(":8080")
if err != nil {
log.Fatalf("Failed to run gin server: %v", err)
}

err = db.FinalizeDatabaseClient()
if err != nil {
log.Fatalf("Failed to finalize database: %v", err)
}
}
4 changes: 3 additions & 1 deletion internal/api/hello_world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"strings"
"testing"

"cloud.google.com/go/firestore"
"github.com/gin-gonic/gin"
"github.com/kumachan-mis/knodeledge-api/internal/db"
"github.com/kumachan-mis/knodeledge-api/internal/model"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
db.InitDatabaseClient("test-project-id")
db.InitDatabaseClient(firestore.DetectProjectID)
m.Run()
db.FinalizeDatabaseClient()
}

func TestHelloWorldHandler(t *testing.T) {
Expand Down
23 changes: 20 additions & 3 deletions internal/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ import (
"context"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
)

var firestoreClient *firestore.Client
var firestoreContext context.Context

func InitDatabaseClient(projectId string) error {
firestoreContext = context.Background()
client, err := firestore.NewClient(firestoreContext, projectId)
func InitDatabaseClient(projectID string) error {
ctx := context.Background()
conf := &firebase.Config{ProjectID: projectID}

app, err := firebase.NewApp(ctx, conf)
if err != nil {
return err
}

client, err := app.Firestore(ctx)
if err != nil {
return err
}

firestoreClient = client
firestoreContext = ctx
return nil
}

func FinalizeDatabaseClient() error {
err := firestoreClient.Close()
firestoreClient = nil
firestoreContext = nil
return err
}

func FirestoreClient() *firestore.Client {
return firestoreClient
}
Expand Down

0 comments on commit 4f9f4d5

Please sign in to comment.