Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from legacy gcm to Firebase #52

Merged
merged 26 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
997be32
WIP
miguelreiswildlife Mar 21, 2024
a483e2d
WIP
miguelreiswildlife Mar 21, 2024
4871619
Set timeout on test
miguelreiswildlife Mar 21, 2024
4378733
Fix locked tests
miguelreiswildlife Mar 22, 2024
7f82aee
WIP
miguelreiswildlife Mar 22, 2024
71f938b
Fix tests
miguelreiswildlife Mar 22, 2024
c7a7997
Now it's fixed
miguelreiswildlife Mar 22, 2024
9569c57
Return PDescribe
miguelreiswildlife Mar 22, 2024
de06d26
Wrap
miguelreiswildlife Mar 22, 2024
c7bfd0a
Change error codes
miguelreiswildlife Mar 22, 2024
965ede5
Add decode hook
miguelreiswildlife Mar 25, 2024
b12db85
Add some debug logs
miguelreiswildlife Mar 25, 2024
5914272
Get credentials from viper directly
miguelreiswildlife Mar 25, 2024
18c7c46
Crash if cannot instantiate message handler for apns
miguelreiswildlife Mar 26, 2024
cdf9171
Remove unused errors
miguelreiswildlife Apr 2, 2024
06bbb11
Better context propagation
miguelreiswildlife Apr 2, 2024
0239c9b
Add nil check
miguelreiswildlife Apr 2, 2024
334ba99
Check if "project_id" field exists in JSON
miguelreiswildlife Apr 2, 2024
3df76f0
Remove unused func param
miguelreiswildlife Apr 2, 2024
2944a82
Add comment and race flag
miguelreiswildlife Apr 2, 2024
5d0eeb6
Add missing ctx param
miguelreiswildlife Apr 2, 2024
9154781
Remove credential
miguelreiswildlife Apr 2, 2024
3cd8556
Fix tests
miguelreiswildlife Apr 2, 2024
e6b2f07
Fix test
miguelreiswildlife Apr 2, 2024
d3b1d00
Fix test
miguelreiswildlife Apr 2, 2024
3d88c07
Return error instead of exiting
miguelreiswildlife Apr 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
miguelreiswildlife marked this conversation as resolved.
Show resolved Hide resolved

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
Loading