-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.go
49 lines (38 loc) · 910 Bytes
/
firebase.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
42
43
44
45
46
47
48
49
package utils
// write a firbase client that can be used globally
import (
"context"
"encoding/json"
"log"
firebase "firebase.google.com/go/v4"
"github.com/izqalan/firehouse/cmd/config"
"google.golang.org/api/option"
)
type FirebaseClient struct {
Client *firebase.App
Context context.Context
}
func NewFirebaseClient() *FirebaseClient {
ctx := context.Background()
credentials, err := config.GetCredentials()
if err != nil {
log.Fatal(err)
}
// parse credentials to json byte
bytes, err := json.Marshal(credentials)
if err != nil {
log.Fatal(err)
}
sa := option.WithCredentialsJSON(bytes)
client, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatal(err)
}
return &FirebaseClient{Client: client, Context: ctx}
}
func (f *FirebaseClient) GetClient() *firebase.App {
return f.Client
}
func (f *FirebaseClient) GetContext() context.Context {
return f.Context
}