Skip to content

Commit

Permalink
Merge pull request #52 from topfreegames/chore/migrate-fcm
Browse files Browse the repository at this point in the history
Migrate from legacy gcm to Firebase
  • Loading branch information
miguelreiswildlife authored Apr 2, 2024
2 parents 43cb7bc + 3d88c07 commit 6b8de27
Show file tree
Hide file tree
Showing 34 changed files with 1,879 additions and 1,146 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MOCKGENERATE := go run github.com/golang/mock/mockgen@v1.7.0-rc.1
MOCKGENERATE := go run go.uber.org/mock/mockgen@v0.4.0
GINKGO := go run github.com/onsi/ginkgo/[email protected]

build:
Expand Down Expand Up @@ -46,7 +46,7 @@ run:
@go run main.go

gcm:
@go run main.go gcm --senderId=test --apiKey=123
@go run main.go gcm

apns:
@go run main.go apns --certificate=./tls/_fixtures/certificate-valid.pem
Expand Down Expand Up @@ -105,7 +105,7 @@ test-unit:
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
@echo
@export $ACK_GINKGO_RC=true
@$(GINKGO) -trace -r --randomizeAllSpecs --randomizeSuites --cover --focus="\[Unit\].*" .
@$(GINKGO) --race -trace -r --randomizeAllSpecs --randomizeSuites --cover --focus="\[Unit\].*" .
@$(MAKE) test-coverage-func
@echo
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
Expand All @@ -120,7 +120,7 @@ run-integration-test:
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
@echo
@export $ACK_GINKGO_RC=true
@$(GINKGO) -trace -r -tags=integration --randomizeAllSpecs --randomizeSuites --focus="\[Integration\].*" .
@$(GINKGO) --race -trace -r -tags=integration --randomizeAllSpecs --randomizeSuites --focus="\[Integration\].*" .
@echo
@echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
@echo "= Integration tests finished. ="
Expand Down Expand Up @@ -171,6 +171,6 @@ integration-test-container-dev: build-image-dev start-deps-container-dev test-db
pusher:local make run-integration-test
@$(MAKE) stop-deps

# .PHONY: mocks
# mocks:
# $(MOCKGENERATE) -package=mocks -source=interfaces/apns.go -destination=mocks/apns.go
.PHONY: mocks
mocks:
$(MOCKGENERATE) -source=interfaces/client.go -destination=mocks/firebase/client.go
5 changes: 4 additions & 1 deletion cmd/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package cmd

import (
"context"
raven "github.com/getsentry/raven-go"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -67,6 +68,8 @@ var apnsCmd = &cobra.Command{
raven.SetDSN(sentryURL)
}

ctx := context.Background()

apnsPusher, err := startApns(debug, json, production, config, nil, nil, nil)
if err != nil {
raven.CaptureErrorAndWait(err, map[string]string{
Expand All @@ -75,7 +78,7 @@ var apnsCmd = &cobra.Command{
})
panic(err)
}
apnsPusher.Start()
apnsPusher.Start(ctx)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/apns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = Describe("APNS", func() {
apnsPusher, err := startApns(false, false, false, config, mockStatsDClient, mockDB, mockPushQueue)
Expect(err).NotTo(HaveOccurred())
Expect(apnsPusher).NotTo(BeNil())
Expect(apnsPusher.Config).NotTo(BeNil())
Expect(apnsPusher.ViperConfig).NotTo(BeNil())
Expect(apnsPusher.IsProduction).To(BeFalse())
Expect(apnsPusher.Logger.Level).To(Equal(logrus.InfoLevel))
Expect(fmt.Sprintf("%T", apnsPusher.Logger.Formatter)).To(Equal(fmt.Sprintf("%T", &logrus.TextFormatter{})))
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
. "github.com/onsi/gomega"
)

func TestExtensions(t *testing.T) {
func TestCMD(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CMD Suite")
}
28 changes: 10 additions & 18 deletions cmd/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,24 @@
package cmd

import (
raven "github.com/getsentry/raven-go"
"context"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/config"
"github.com/topfreegames/pusher/interfaces"
"github.com/topfreegames/pusher/pusher"
"github.com/topfreegames/pusher/util"
)

var senderID string
var apiKey string

func startGcm(
ctx context.Context,
debug, json, production bool,
senderID, apiKey string,
config *viper.Viper,
vConfig *viper.Viper,
config *config.Config,
statsdClientOrNil interfaces.StatsDClient,
dbOrNil interfaces.DB,
clientOrNil interfaces.GCMClient,
) (*pusher.GCMPusher, error) {
var log = logrus.New()
if json {
Expand All @@ -52,7 +51,7 @@ func startGcm(
} else {
log.Level = logrus.InfoLevel
}
return pusher.NewGCMPusher(production, config, log, statsdClientOrNil, dbOrNil, clientOrNil)
return pusher.NewGCMPusher(ctx, production, vConfig, config, log, statsdClientOrNil)
}

// gcmCmd represents the gcm command
Expand All @@ -61,25 +60,18 @@ var gcmCmd = &cobra.Command{
Short: "starts pusher in gcm mode",
Long: `starts pusher in gcm mode`,
Run: func(cmd *cobra.Command, args []string) {
config, err := util.NewViperWithConfigFile(cfgFile)
config, vConfig, err := config.NewConfigAndViper(cfgFile)
if err != nil {
panic(err)
}

sentryURL := config.GetString("sentry.url")
if sentryURL != "" {
raven.SetDSN(sentryURL)
}
ctx := context.Background()

gcmPusher, err := startGcm(debug, json, production, senderID, apiKey, config, nil, nil, nil)
gcmPusher, err := startGcm(ctx, debug, json, production, vConfig, config, nil)
if err != nil {
raven.CaptureErrorAndWait(err, map[string]string{
"version": util.Version,
"cmd": "gcm",
})
panic(err)
}
gcmPusher.Start()
gcmPusher.Start(ctx)
},
}

Expand Down
91 changes: 0 additions & 91 deletions cmd/gcm_test.go

This file was deleted.

3 changes: 0 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ import (
var _ = Describe("Root", func() {
Describe("[Unit]", func() {
It("Should return help", func() {
err := RootCmd.Execute()
Expect(err.Error()).To(ContainSubstring("unknown flag: --test.timeout"))

r, w, _ := os.Pipe()
RootCmd.SetArgs([]string{})
RootCmd.SetOutput(w)
Expand Down
88 changes: 88 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package config

import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/topfreegames/pusher/util"
"reflect"
"strings"
)

type (
// Config is the struct that holds all the configuration for the Pusher.
Config struct {
GCM GCM
GracefulShutdownTimeout int
}

GCM struct {
Apps string
PingInterval int
PingTimeout int
MaxPendingMessages int
LogStatsInterval int
}
)

// NewConfigAndViper returns a new Config object and the corresponding viper instance.
func NewConfigAndViper(configFile string) (*Config, *viper.Viper, error) {
v, err := util.NewViperWithConfigFile(configFile)
if err != nil {
return nil, nil, err
}

if err := v.ReadInConfig(); err != nil {
return nil, nil, fmt.Errorf("error reading config file from %s: %s", configFile, err)
}

config := &Config{}
if err := v.Unmarshal(config, decodeHookFunc()); err != nil {
return nil, nil, fmt.Errorf("error unmarshalling config: %s", err)
}

return config, v, nil
}

func (c *Config) GetAppsArray() []string {
arr := strings.Split(c.GCM.Apps, ",")
res := make([]string, 0, len(arr))
for _, a := range arr {
res = append(res, strings.TrimSpace(a))
}

return res
}

func decodeHookFunc() viper.DecoderConfigOption {
hooks := mapstructure.ComposeDecodeHookFunc(
StringToMapStringHookFunc(),
)
return viper.DecodeHook(hooks)
}

func StringToMapStringHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String || t.Kind() != reflect.Map {
return data, nil
}

if t.Key().Kind() != reflect.String || t.Elem().Kind() != reflect.String {
return data, nil
}

raw := data.(string)
if raw == "" {
return map[string]string{}, nil
}

m := map[string]string{}
err := json.Unmarshal([]byte(raw), &m)
return m, err
}
}
4 changes: 3 additions & 1 deletion config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ gcm:
pingTimeout: 10
maxPendingMessages: 100
logStatsInterval: 10000
apps: "game"
apps: mygame
certs:
game:
apiKey: game-api-key
senderID: "1233456789"
firebaseCredentials:
mygame: "{}"
queue:
topics:
- "^push-[^-_]+_(apns|gcm)[_-](single|massive)"
Expand Down
Loading

0 comments on commit 6b8de27

Please sign in to comment.