From 700a1f700ba91d5daf093783e283278f9b1e5571 Mon Sep 17 00:00:00 2001 From: kish1n Date: Mon, 23 Sep 2024 16:17:39 +0300 Subject: [PATCH] change cfg remake notification process --- config.yaml | 12 ++- internal/config/creds.go | 32 ++------ internal/config/main.go | 5 +- internal/service/handlers/ctx.go | 6 +- internal/service/workers/cron/main.go | 2 +- .../workers/notificationdailyquestion/main.go | 56 ++++++++++++-- .../notificationdailyquestion/script.go | 74 ------------------- 7 files changed, 75 insertions(+), 112 deletions(-) delete mode 100644 internal/service/workers/notificationdailyquestion/script.go diff --git a/config.yaml b/config.yaml index b585def..a40b1f3 100644 --- a/config.yaml +++ b/config.yaml @@ -138,4 +138,14 @@ poll_verifier: sig_verifier: verification_key: "37bc75afc97f8bdcd21cda85ae7b2885b5f1205ae3d79942e56457230f1636a037cc7ebfe42998d66a3dd3446b9d29366271b4f2bd8e0d307db1d320b38fc02f" -creds: "path/to/creds" \ No newline at end of file +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" \ No newline at end of file diff --git a/internal/config/creds.go b/internal/config/creds.go index ca7fc61..35e13d0 100644 --- a/internal/config/creds.go +++ b/internal/config/creds.go @@ -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) } diff --git a/internal/config/main.go b/internal/config/main.go index f8f082f..fd47597 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -21,11 +21,11 @@ type Config interface { hmacsig.SigCalculatorProvider PollVerifierer Rarimarket - Creds Levels() *Levels Verifiers() Verifiers DailyQuestions() *DailyQuestions + Creds() *Creds } type config struct { @@ -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 @@ -61,6 +61,5 @@ func New(getter kv.Getter) Config { EventTypeser: evtypes.NewConfig(getter), SigCalculatorProvider: hmacsig.NewCalculatorProvider(getter), Rarimarket: NewRarimarketConfig(getter), - Creds: NewCreds(getter), } } diff --git a/internal/service/handlers/ctx.go b/internal/service/handlers/ctx.go index 71a9360..8a87c3e 100644 --- a/internal/service/handlers/ctx.go +++ b/internal/service/handlers/ctx.go @@ -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 { diff --git a/internal/service/workers/cron/main.go b/internal/service/workers/cron/main.go index 5a29527..92f6f41 100644 --- a/internal/service/workers/cron/main.go +++ b/internal/service/workers/cron/main.go @@ -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 diff --git a/internal/service/workers/notificationdailyquestion/main.go b/internal/service/workers/notificationdailyquestion/main.go index 7271bcc..4071bb1 100644 --- a/internal/service/workers/notificationdailyquestion/main.go +++ b/internal/service/workers/notificationdailyquestion/main.go @@ -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()) @@ -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) +} diff --git a/internal/service/workers/notificationdailyquestion/script.go b/internal/service/workers/notificationdailyquestion/script.go deleted file mode 100644 index e8161b9..0000000 --- a/internal/service/workers/notificationdailyquestion/script.go +++ /dev/null @@ -1,74 +0,0 @@ -package notificationdailyquestion - -import ( - "context" - "encoding/json" - "flag" - "fmt" - "log" - "os" - - firebase "firebase.google.com/go/v4" - "firebase.google.com/go/v4/messaging" - "google.golang.org/api/option" -) - -func getMessage(msgFile string) (*messaging.Message, error) { - file, err := os.Open(msgFile) - if err != nil { - return nil, fmt.Errorf("failed to open file: %w", err) - } - defer file.Close() - - data := &struct { - Msg messaging.Message `json:"message"` - }{} - - decoder := json.NewDecoder(file) - if err = decoder.Decode(data); err != nil { - return nil, fmt.Errorf("failed to decode message: %w", err) - } - - return &data.Msg, nil -} - -func Script(toCreds string) { - credFile := flag.String("file", toCreds, "Path to file with Firebase service account credentionals in json") - msgFile := flag.String("msg", "msg.json", "Path to file with message. https://firebase.google.com/docs/cloud-messaging/concept-options") - flag.Parse() - - msg, err := getMessage(*msgFile) - if err != nil { - log.Fatalf("failed to get message: %v\n", err) - } - msg.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("Sucess: %s\n", response) -}