-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
41 lines (32 loc) · 895 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Package db provides firestore client
package db
import (
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"fmt"
"golang.org/x/net/context"
"google.golang.org/api/option"
"os"
)
var client *firestore.Client
func getOption() option.ClientOption {
rawString := os.Getenv("SERVICE_ACCOUNT_KEY")
return option.WithCredentialsJSON([]byte(rawString))
}
// GetClient return signletone firestore instance
func GetClient() (*firestore.Client, error) {
if client == nil {
ctx := context.Background()
conf := &firebase.Config{ProjectID: "festa-notify"}
app, err := firebase.NewApp(ctx, conf, getOption())
if err != nil {
return nil, fmt.Errorf("error initializing firebase app: %v", err)
}
store, err := app.Firestore(ctx)
if err != nil {
return nil, fmt.Errorf("error initializing firestore: %v", err)
}
client = store
}
return client, nil
}