/
firebase.go
71 lines (63 loc) · 1.65 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package services
import (
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"firebase.google.com/go/messaging"
log "github.com/shyyawn/go-to/x/logging"
"github.com/shyyawn/go-to/x/source"
"github.com/spf13/viper"
"google.golang.org/api/option"
)
type Firebase struct {
ProjectId string `mapstructure:"project_id"`
CredentialFile string `mapstructure:"credential_file"`
app *firebase.App
messagingClient *messaging.Client
authClient *auth.Client
Cancel *context.CancelFunc
Ctx *context.Context
}
func (svc *Firebase) LoadFromConfig(key string, config *viper.Viper) error {
return source.LoadFromConfig(key, config, svc)
}
func (svc *Firebase) App() *firebase.App {
if svc.app != nil {
return svc.app
}
ctx, cancel := context.WithCancel(context.Background())
app, err := firebase.NewApp(ctx, &firebase.Config{
ProjectID: svc.ProjectId,
}, option.WithCredentialsFile(svc.CredentialFile))
if err != nil {
cancel()
log.Fatal(500, "Error initialization app", err)
}
// Set for later reference
svc.Ctx = &ctx
svc.Cancel = &cancel
svc.app = app
return svc.app
}
func (svc *Firebase) Messaging() *messaging.Client {
if svc.messagingClient != nil {
return svc.messagingClient
}
client, err := svc.App().Messaging(*svc.Ctx)
if err != nil {
(*svc.Cancel)()
log.Fatal(500, "Error getting Messaging client", err)
}
return client
}
func (svc *Firebase) Auth() *auth.Client {
if svc.authClient != nil {
return svc.authClient
}
client, err := svc.App().Auth(*svc.Ctx)
if err != nil {
(*svc.Cancel)()
log.Fatal(500, "Error getting Messaging client", err)
}
return client
}