-
Notifications
You must be signed in to change notification settings - Fork 13
/
fcm.go
48 lines (39 loc) · 1.18 KB
/
fcm.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
package registry
import (
"os"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
"github.com/coretrix/hitrix/service/component/config"
"github.com/coretrix/hitrix/service/component/fcm"
)
const (
fcmConfigEnvName = "FIREBASE_CONFIG"
googleAuthConfigEnvName = "GOOGLE_APPLICATION_CREDENTIALS"
)
func ServiceProviderFCM() *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: service.FCMService,
Build: func(ctn di.Container) (interface{}, error) {
val, present := os.LookupEnv(fcmConfigEnvName)
configService := ctn.Get(service.ConfigService).(config.IConfig)
if present {
if _, err := os.Stat(val); err != nil {
if os.IsNotExist(err) {
// specified config file doesn't exists
credentialsFile := configService.GetFolderPath() + "/.fcm.json"
err := os.Setenv(fcmConfigEnvName, credentialsFile)
if err != nil {
return nil, err
}
err = os.Setenv(googleAuthConfigEnvName, credentialsFile)
if err != nil {
return nil, err
}
}
}
}
return fcm.NewFCM(ctn.Get(service.AppService).(*app.App).GlobalContext)
},
}
}