Skip to content

Commit

Permalink
change cfg remake notification process
Browse files Browse the repository at this point in the history
  • Loading branch information
kish1n committed Sep 23, 2024
1 parent 4a038d7 commit 700a1f7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 112 deletions.
12 changes: 11 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ poll_verifier:
sig_verifier:
verification_key: "37bc75afc97f8bdcd21cda85ae7b2885b5f1205ae3d79942e56457230f1636a037cc7ebfe42998d66a3dd3446b9d29366271b4f2bd8e0d307db1d320b38fc02f"

creds: "path/to/creds"
rarimarket:
rpc: "https://rpc.evm.node1.mainnet-beta.rarimo.com:443"
account_factory: "0x0000000000000000000000000000000000000001"
point_tokens: "0x0000000000000000000000000000000000000002"
point_price: "1000000000"
vault_address: "https://vault.example.com"
vault_mount_path: "secret/rarimarket"
private_key: "4c0883a69102937d6231471b5dbb6204fe5129617082796eb028488023b662c5"

creds:
path: "path/to/creds"
32 changes: 7 additions & 25 deletions internal/config/creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,24 @@ package config
import (
"fmt"
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/comfig"
"gitlab.com/distributed_lab/kit/kv"
)

type Creds interface {
Creds() *CredsDetails
type Creds struct {
Path string `fig:"path"`
}

func NewCreds(getter kv.Getter) Creds {
return &credsDetails{
getter: getter,
}
}

type credsDetails struct {
once comfig.Once
getter kv.Getter
}

type CredsDetails struct {
Path string `fig:"creds,required"`
}

func (c *credsDetails) Creds() *CredsDetails {
return c.once.Do(func() interface{} {

var cfg CredsDetails
func (c *config) Creds() *Creds {
return c.Cred.Do(func() interface{} {
var cfg Creds

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "creds")).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure creads: %w", err))
panic(fmt.Errorf("failed to figure creds: %w", err))
}

return &cfg

}).(*CredsDetails)
}).(*Creds)
}
5 changes: 2 additions & 3 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ type Config interface {
hmacsig.SigCalculatorProvider
PollVerifierer
Rarimarket
Creds

Levels() *Levels
Verifiers() Verifiers
DailyQuestions() *DailyQuestions
Creds() *Creds
}

type config struct {
Expand All @@ -38,10 +38,10 @@ type config struct {
hmacsig.SigCalculatorProvider
PollVerifierer
Rarimarket
Creds

passport root.VerifierProvider

Cred comfig.Once
DailyQuestion comfig.Once
levels comfig.Once
verifier comfig.Once
Expand All @@ -61,6 +61,5 @@ func New(getter kv.Getter) Config {
EventTypeser: evtypes.NewConfig(getter),
SigCalculatorProvider: hmacsig.NewCalculatorProvider(getter),
Rarimarket: NewRarimarketConfig(getter),
Creds: NewCreds(getter),
}
}
6 changes: 3 additions & 3 deletions internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ func PollVerifier(r *http.Request) *config.PollVerifier {
return r.Context().Value(voteVerifierCtxKey).(*config.PollVerifier)
}

func CtxCreds(creds *config.CredsDetails) func(context.Context) context.Context {
func CtxCreds(creds *config.Creds) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, credsCtxKey, creds)
}
}

func Creds(r *http.Request) *config.CredsDetails {
return r.Context().Value(credsCtxKey).(*config.CredsDetails)
func Creds(r *http.Request) *config.Creds {
return r.Context().Value(credsCtxKey).(*config.Creds)
}

func CtxSigCalculator(calc hmacsig.Calculator) func(context.Context) context.Context {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/workers/cron/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// how many services should call Start to do the actual start
const countOfServices = 3
const countOfServices = 4

var sin = struct {
scheduler gocron.Scheduler
Expand Down
56 changes: 51 additions & 5 deletions internal/service/workers/notificationdailyquestion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package notificationdailyquestion

import (
"context"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"fmt"
"github.com/go-co-op/gocron/v2"
"github.com/rarimo/geo-points-svc/internal/config"
"github.com/rarimo/geo-points-svc/internal/data/pg"
"github.com/rarimo/geo-points-svc/internal/service/workers/cleanquestiondeadlines"
"github.com/rarimo/geo-points-svc/internal/service/workers/cron"
"google.golang.org/api/option"
"log"
"time"
)

func Run(ctx context.Context, cfg config.Config, sig chan struct{}) {
cron.Init(cfg.Log())
log := cfg.Log().WithField("who", "daily-questions-cleaner")
logger := cfg.Log().WithField("notification", "daily-questions-notification")

questionsQ := pg.NewDailyQuestionsQ(cfg.DB().Clone())

Expand All @@ -23,21 +27,63 @@ func Run(ctx context.Context, cfg config.Config, sig chan struct{}) {
gocron.NewTask(func() {
curQuestion, err := questionsQ.FilterDayQuestions(time.Now().UTC()).Get()
if err != nil {
log.Fatalf("error getting daily question: %v", err)
logger.Fatalf("error getting daily question: %v", err)
return
}
if curQuestion == nil {
log.Infof("There's no daily question today")
logger.Infof("There's no daily question today")
return
}
Script(cfg.Creds().Path)
sendingNotifications(cfg.Creds().Path)
}),
gocron.WithName("daily-questions-notification"),
)
if err != nil {
panic(fmt.Errorf("failed to initialize daily job: %w", err))
}

sig <- struct{}{}

cron.Start(ctx)
}

func sendingNotifications(toCreds string) {
credFile := toCreds

msg := &messaging.Message{
Notification: &messaging.Notification{
Title: "Daily Question",
Body: "The new daily question is finally available",
},
Topic: "daily-questions",
APNS: &messaging.APNSConfig{
Headers: map[string]string{
"apns-priority": "10",
},
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
MutableContent: true,
},
},
},
}

fmt.Printf("%+v\n", msg)

ctx := context.Background()
app, err := firebase.NewApp(ctx, nil, option.WithCredentialsFile(credFile))
if err != nil {
log.Fatalf("failed to initialize app: %v\n", err)
}

client, err := app.Messaging(ctx)
if err != nil {
log.Fatalf("failed to get Messaging client: %v\n", err)
}

response, err := client.Send(ctx, msg)
if err != nil {
log.Fatalf("failed to send message: %v\n", err)
}

log.Printf("Success: %s\n", response)
}
74 changes: 0 additions & 74 deletions internal/service/workers/notificationdailyquestion/script.go

This file was deleted.

0 comments on commit 700a1f7

Please sign in to comment.