/
client.go
35 lines (33 loc) · 1.07 KB
/
client.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
package firebase
import (
"context"
"encoding/base64"
firebase "firebase.google.com/go"
"github.com/justdomepaul/toolbox/config"
"google.golang.org/api/option"
)
// NewClient method
// env FIREBASE_CONFIG_JSON
// env FIREBASE_PROJECT_ID
func NewClient(ctx context.Context, firebaseOption config.Firebase) (*firebase.App, error) {
options := make([]option.ClientOption, 0)
if firebaseOption.FirebaseConfigJSON != "" {
options = append(options, option.WithCredentialsJSON([]byte(firebaseOption.FirebaseConfigJSON)))
}
if firebaseOption.FirebaseConfigJSONBase64 != "" {
firebaseConfigJSON, err := base64.StdEncoding.DecodeString(firebaseOption.FirebaseConfigJSONBase64)
if err != nil {
return nil, err
}
options = append(options, option.WithCredentialsJSON(firebaseConfigJSON))
}
var cf *firebase.Config
if (firebaseOption.FirebaseConfigJSON == "" ||
firebaseOption.FirebaseConfigJSONBase64 == "") &&
firebaseOption.FirebaseProjectID != "" {
cf = &firebase.Config{
ProjectID: firebaseOption.FirebaseProjectID,
}
}
return firebase.NewApp(ctx, cf, options...)
}