Skip to content

Commit

Permalink
moving firebase objects to global
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandosborne committed Sep 16, 2024
1 parent bf1fed3 commit c8a7435
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
17 changes: 3 additions & 14 deletions net/repeater/internal/api_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package repeater
import (
"encoding/json"
"log"
"context"
"net/http"
"os"
"runtime"
"strings"
"fmt"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
)

Expand All @@ -34,16 +32,6 @@ func WriteResponse(w http.ResponseWriter, v interface{}) {

//Notify proxies push notification to device
func Notify(w http.ResponseWriter, r *http.Request) {
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}

ctx := context.Background()
client, err := app.Messaging(ctx)
if err != nil {
log.Fatalf("error getting Messaging client: %v\n", err)
}

var msg PushMessage
if err := ParseRequest(r, w, &msg); err != nil {
Expand All @@ -60,9 +48,10 @@ func Notify(w http.ResponseWriter, r *http.Request) {

// Send a message to the device corresponding to the provided
// registration token.
response, err := client.Send(ctx, message)
response, err := FCMClient.Send(FCMContext, message)
if err != nil {
log.Fatalln(err)
ErrResponse(w, http.StatusBadRequest, err)
return
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
Expand Down
20 changes: 20 additions & 0 deletions net/repeater/internal/routers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package repeater

import (
"firebase.google.com/go/v4/messaging"
firebase "firebase.google.com/go/v4"
"github.com/gorilla/mux"
"net/http"
"strings"
"context"
"log"
)

type route struct {
Expand All @@ -15,9 +19,25 @@ type route struct {

type routes []route

var FCMClient *messaging.Client
var FCMContext context.Context

//NewRouter allocate router for databag API
func NewRouter() *mux.Router {

app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}

ctx := context.Background()
client, err := app.Messaging(ctx)
if err != nil {
log.Fatalf("error getting Messaging client: %v\n", err)
}
FCMClient = client
FCMContext = ctx

router := mux.NewRouter().StrictSlash(true)
for _, route := range endpoints {
var handler http.Handler
Expand Down

0 comments on commit c8a7435

Please sign in to comment.