68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package clients
|
|
|
|
import (
|
|
"context"
|
|
|
|
firebase "firebase.google.com/go/v4"
|
|
"firebase.google.com/go/v4/messaging"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/sideshow/apns2"
|
|
"github.com/sideshow/apns2/token"
|
|
"github.com/spf13/viper"
|
|
"google.golang.org/api/option"
|
|
)
|
|
|
|
var (
|
|
apnsClient *apns2.Client
|
|
firebaseClient *messaging.Client
|
|
)
|
|
|
|
func InitPushClients() {
|
|
// Initialize APNs Client
|
|
apnsCertPath := viper.GetString("apns.certificate_path")
|
|
apnsKeyID := viper.GetString("apns.key_id")
|
|
apnsTeamID := viper.GetString("apns.team_id")
|
|
|
|
if apnsCertPath != "" && apnsKeyID != "" && apnsTeamID != "" {
|
|
authKey, err := token.AuthKeyFromFile(apnsCertPath)
|
|
token := &token.Token{
|
|
AuthKey: authKey,
|
|
KeyID: apnsKeyID,
|
|
TeamID: apnsTeamID,
|
|
}
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Failed to create APNs auth key")
|
|
}
|
|
apnsClient = apns2.NewTokenClient(token)
|
|
apnsClient.Production() // Use Production environment
|
|
log.Info().Msg("APNs client initialized in production mode")
|
|
} else {
|
|
log.Warn().Msg("APNs configuration missing. Skipping APNs client initialization.")
|
|
}
|
|
|
|
// Initialize Firebase Client
|
|
firebaseServiceAccountPath := viper.GetString("firebase.service_account_path")
|
|
if firebaseServiceAccountPath != "" {
|
|
opt := option.WithCredentialsFile(firebaseServiceAccountPath)
|
|
app, err := firebase.NewApp(context.Background(), nil, opt)
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Failed to create Firebase app")
|
|
}
|
|
firebaseClient, err = app.Messaging(context.Background())
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Failed to create Firebase Messaging client")
|
|
}
|
|
log.Info().Msg("Firebase Messaging client initialized")
|
|
} else {
|
|
log.Warn().Msg("Firebase service account path missing. Skipping Firebase client initialization.")
|
|
}
|
|
}
|
|
|
|
func GetAPNsClient() *apns2.Client {
|
|
return apnsClient
|
|
}
|
|
|
|
func GetFirebaseClient() *messaging.Client {
|
|
return firebaseClient
|
|
}
|