forked from acoshift/go-firebase-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
77 lines (66 loc) · 1.66 KB
/
app.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
72
73
74
75
76
77
package admin
import (
"context"
"crypto/rsa"
"net/http"
jwtgo "github.com/dgrijalva/jwt-go"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)
// App holds information about application configuration
type App struct {
projectID string
jwtConfig *jwt.Config
privateKey *rsa.PrivateKey
databaseURL string
client *http.Client
apiKey string
}
// AppOptions is the firebase app options for initialize app
type AppOptions struct {
ProjectID string
ServiceAccount []byte
DatabaseURL string
APIKey string
}
// InitializeApp initializes firebase application with options
func InitializeApp(ctx context.Context, options AppOptions) (*App, error) {
var err error
app := App{
projectID: options.ProjectID,
databaseURL: options.DatabaseURL,
apiKey: options.APIKey,
}
if options.ServiceAccount != nil {
app.jwtConfig, err = google.JWTConfigFromJSON(options.ServiceAccount, scopes...)
if err != nil {
return nil, err
}
app.privateKey, err = jwtgo.ParseRSAPrivateKeyFromPEM(app.jwtConfig.PrivateKey)
if err != nil {
return nil, err
}
app.client = app.jwtConfig.Client(ctx)
} else {
app.client, err = google.DefaultClient(ctx, scopes...)
if err != nil {
return nil, err
}
}
return &app, nil
}
// Auth creates new Auth instance
// each instance has the same firebase app instance
// but difference public keys instance
// better create only one instance
func (app *App) Auth() *Auth {
return newAuth(app)
}
// Database creates new Database instance
func (app *App) Database() *Database {
return newDatabase(app)
}
// FCM creates new FCM instance
func (app *App) FCM() *FCM {
return newFCM(app)
}