From ee30e63bb67200f3a714820c956a9fc71f57aaf4 Mon Sep 17 00:00:00 2001 From: jarrel Date: Tue, 8 Oct 2024 09:00:44 -0700 Subject: [PATCH] Add moshi push handler (#1539) --- pushnotifications/pushnotifications.go | 22 ++++++++++++++++++- .../local/app-local-pushnotifications.yaml | 8 +++++-- secrets/prod/pushnotifications-env.yaml | 9 ++++++-- service/persist/postgres/postgres.go | 15 ++++++++++++- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pushnotifications/pushnotifications.go b/pushnotifications/pushnotifications.go index 7fb57f9eb..cc3752acb 100644 --- a/pushnotifications/pushnotifications.go +++ b/pushnotifications/pushnotifications.go @@ -4,6 +4,7 @@ import ( "context" "github.com/getsentry/sentry-go" "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v4/pgxpool" db "github.com/mikeydub/go-gallery/db/gen/coredb" "github.com/mikeydub/go-gallery/env" "github.com/mikeydub/go-gallery/middleware" @@ -63,11 +64,18 @@ func CoreInitServer(ctx context.Context) *gin.Engine { apiURL := env.GetString("EXPO_PUSH_API_URL") accessToken := env.GetString("EXPO_PUSH_ACCESS_TOKEN") - expoHandler := expo.NewPushNotificationHandler(ctx, queries, apiURL, accessToken) + expoHandler := expo.NewPushNotificationHandler(ctx, queries, apiURL, accessToken) taskGroup.POST("send-push-notification", sendPushNotificationHandler(expoHandler)) jobGroup.POST("check-push-tickets", checkPushTicketsHandler(expoHandler)) + // Moshi handlers + moshiDB := newMoshiDB() + moshiQueries := db.New(moshiDB) + moshiHandler := expo.NewPushNotificationHandler(ctx, moshiQueries, apiURL, accessToken) + router.POST("moshi/tasks/send-push-notification", sendPushNotificationHandler(moshiHandler)) + router.POST("moshi/jobs/check-push-tickets", checkPushTicketsHandler(moshiHandler)) + return router } @@ -122,6 +130,18 @@ func checkPushTicketsHandler(expoHandler *expo.PushNotificationHandler) gin.Hand } } +func newMoshiDB() *pgxpool.Pool { + return postgres.NewPgxClient( + postgres.WithUser(env.GetString("MOSHI_POSTGRES_USER")), + postgres.WithPassword(env.GetString("MOSHI_POSTGRES_PASSWORD")), + postgres.WithDBName(env.GetString("MOSHI_POSTGRES_DB")), + postgres.WithHost(env.GetString("MOSHI_POSTGRES_HOST")), + postgres.WithPort(env.GetInt("MOSHI_POSTGRES_PORT")), + postgres.WithRetries(postgres.DefaultConnectRetry), + postgres.WithAppName("pushnotifications"), + ) +} + func setDefaults() { viper.SetDefault("ENV", "local") viper.SetDefault("POSTGRES_HOST", "0.0.0.0") diff --git a/secrets/local/local/app-local-pushnotifications.yaml b/secrets/local/local/app-local-pushnotifications.yaml index fb72c92ec..cab244363 100644 --- a/secrets/local/local/app-local-pushnotifications.yaml +++ b/secrets/local/local/app-local-pushnotifications.yaml @@ -1,4 +1,8 @@ EXPO_PUSH_ACCESS_TOKEN: ENC[AES256_GCM,data:CQ4uTHvtybMdmeFs7/Z6czXvqForCaswLag1aefjG1Rj1Mbq5df+kA==,iv:4iNa4IgBVSgQ0qCVj3inQlKC2mshvR3TIkNknR3fus4=,tag:B2JIqQb5KY0Jo57wuf6t6w==,type:str] +MOSHI_POSTGRES_USER: ENC[AES256_GCM,data:hXzH9iy88zTCqr5Mrt8r,iv:ykHWVBMZfiqvCD+6qrZFYHT2qZGZYC7VSzWb4sQmI08=,tag:vJs9tKHmqW9ZSImhJ6YfHQ==,type:str] +MOSHI_POSTGRES_DB: ENC[AES256_GCM,data:TE1fje3fvz4=,iv:M1d2Iu+9Cdk999t4ICzorHb7zYCwRvEtrvmyygyLkuE=,tag:QFYDoRonX92omMvgwL0myA==,type:str] +MOSHI_POSTGRES_HOST: ENC[AES256_GCM,data:BEMu6mEFaQEE,iv:vX+CMR6pG9qnVG7ZbSgjHlqmGfsS7gaprHT2BvN3EZQ=,tag:g+a4dGZjyV2d2qasXyDXYg==,type:str] +MOSHI_POSTGRES_PORT: ENC[AES256_GCM,data:Bu/woA==,iv:3i/DpWPjSFXqY2FTnHWdiufh9FrbFk7hZ8Fqqyc3xmA=,tag:xnHPobnM7V/jfj6kXSD2Mg==,type:str] sops: kms: [] gcp_kms: @@ -8,8 +12,8 @@ sops: azure_kv: [] hc_vault: [] age: [] - lastmodified: "2023-04-27T18:18:09Z" - mac: ENC[AES256_GCM,data:I7yGSGT4GvvQxmw2g7X59A8MgY2Z2bGoW8LUV7jZvOWuPWieb0Y09kXK9DC/HBLgZYUE040DXe6BNnTP3Nr792C/o4x6mQjR2WbzDicg/F9vSy+IlhcAo0BZxyg3ruzlXsxgHPMw6jJ7KyAuq6vKyLbMw6i48YIEBGFDZG6PyYE=,iv:JeJmI0jauWNFpnyTGjei39y7X6wZAcNCz6Mm7I/0Lf0=,tag:El9y9nI72eaDNBxPl3EKpg==,type:str] + lastmodified: "2024-10-08T00:57:02Z" + mac: ENC[AES256_GCM,data:NmpWbi31zsxGcKHI5McS9EZfGBt3uyh59mpioohGAcdZRMJ0gzp+1POwCF6o8GUDsClHx2VAyMxGxle1U88gb/1YVoBik2ZJ9YU/4L+WQwWy7S0lvI702OXDO9lrbjQNL4NtXlnEcPzkxiuf5HrXgyOnOpOI2iM72Bvwz+eNoFY=,iv:MP7L/cwE/g5NYaNFw6VDoXyd1OncSZaCi5UVcuir6V0=,tag:LF+6Pb8nX0HcSKaD5ETwvA==,type:str] pgp: [] unencrypted_suffix: _unencrypted version: 3.7.3 diff --git a/secrets/prod/pushnotifications-env.yaml b/secrets/prod/pushnotifications-env.yaml index c048b7093..5632c2f79 100644 --- a/secrets/prod/pushnotifications-env.yaml +++ b/secrets/prod/pushnotifications-env.yaml @@ -8,6 +8,11 @@ SENTRY_DSN: ENC[AES256_GCM,data:Mk106uzhxkdFI8k9nogUe1i2h4q5XGKe8/QkLYzn2jFzIsZ8 SENTRY_TRACES_SAMPLE_RATE: ENC[AES256_GCM,data:jvxj,iv:toUdllVx4KrCeVOXPHvNL3Aq6r6j/aR4j2tneU1GTQ4=,tag:XrlUNBDX0qW0KRlaQO6LZw==,type:str] PUSH_NOTIFICATIONS_SECRET: ENC[AES256_GCM,data:IWKkCB7bftW/Di5QVMk564052SGXG4fBm6uLh92N/Kw=,iv:DwJOtS7CLKvdv/+8RPffm2WwpQyB9IcIN/zzDzKaBu0=,tag:k8EqVcuGh/ZYd/SnVyQxkQ==,type:str] EXPO_PUSH_ACCESS_TOKEN: ENC[AES256_GCM,data:CSUA1tp7EXXhbsY47tTN/Omh9HOfgqrlrIqrgws06LNTqT3i7SH2Nw==,iv:8KY1AydvmiyAcUtut9eOdsiRpWNWYmUn4vpvYpiQzZ8=,tag:1z808Iix155TnezleVKw4Q==,type:str] +MOSHI_POSTGRES_USER: ENC[AES256_GCM,data:M9e8pFweYEm14F/84Gxn,iv:26Gi+efwPA5Eiydv/LxFH26qJgZ11EzuMiALh5vRwBk=,tag:s+AqKPiQ9XeJfK+tqKnKMw==,type:str] +MOSHI_POSTGRES_PASSWORD: ENC[AES256_GCM,data:iVXfH1nSKyQahhmL5UAaETNN9HPOAdAPpn8NxH75Zh8=,iv:jljHAsQZ59RqoArh2GM84oAIfFFsc/5Hao04AbA5Faw=,tag:+SNtIoPq+YuZtdNK5+0HnQ==,type:str] +MOSHI_POSTGRES_DB: ENC[AES256_GCM,data:t2WCpWAT15Q=,iv:leM7X0NdOQgulVG0hhq55AO7QvOhIB3MgfLY2GX3ZAc=,tag:RJdMQjKLpn0yxzcnN2SUgg==,type:str] +MOSHI_POSTGRES_HOST: ENC[AES256_GCM,data:HIbK/8oIJYcNf1xZkRb66YgGlIDMPyxux9X7z2biqi9sj/bfm/fg++4F1C2c0HPTFskdZEB7gwWV,iv:J7kG9nE21fkcPpuIssb+0mwRxdceAjZc5QRMTvmWIwM=,tag:qt3bnPZecSqF3D4ayCTh+Q==,type:str] +MOSHI_POSTGRES_PORT: ENC[AES256_GCM,data:4T10TQ==,iv:Rzjh4XogLzIQ/f6MlykK3NM1OVhoHHgd7tGBMR5SjLM=,tag:LVzfMgUv3lqTMdxQmprieQ==,type:str] sops: kms: [] gcp_kms: @@ -17,8 +22,8 @@ sops: azure_kv: [] hc_vault: [] age: [] - lastmodified: "2024-03-15T18:08:15Z" - mac: ENC[AES256_GCM,data:BgrRm+DbjnES+U+pxa0fTJvVACHjiC3/yoeB0jCWm0RgslldJ2QRsH0rkCcJFijrgE030LV9gIcTBU0x1ko2LAasyF1FfoBFLxoyLHtPJQ5ZPvBa9yomAGnhLRpizkWaNNQfb9JJSnLQlDDupoaQlwJwG0IhqzVMETe7rBox5SQ=,iv:8HqKS2lIpnCgG6C7wS61DPIW7HHkUO4jShIHkw5MIVU=,tag:pd5HMBOVI1udxvnEz76J3A==,type:str] + lastmodified: "2024-10-08T00:58:09Z" + mac: ENC[AES256_GCM,data:9CUf5Tu3/LbZIZ8FVRMobYFTCzfbA4azQdPOLY15E9gdInHDISFAFr4FGMumkCQxDbDdjZoXUqfKQ1aezAs5/u9mnndEKbshn/aDuut4ZM6Oh6RoEi97zMxxtShEM14aQPsVR3EI1LCkwaRlEkxn7P49a60ci6uPI+Rfucvj2kQ=,iv:9Q1Dodj35l/To0sTwcI6BK8LK88cZzuE18dX46sbVW0=,tag:VM1MBrdeODnczyI3F2NUew==,type:str] pgp: [] unencrypted_suffix: _unencrypted version: 3.7.3 diff --git a/service/persist/postgres/postgres.go b/service/persist/postgres/postgres.go index d7f171587..f2e159238 100644 --- a/service/persist/postgres/postgres.go +++ b/service/persist/postgres/postgres.go @@ -23,6 +23,8 @@ import ( // _ "github.com/lib/pq" ) +var DefaultConnectRetry = retry.Retry{MinWait: 2, MaxWait: 4, MaxRetries: 3} + type ErrRoleDoesNotExist struct { role string } @@ -37,6 +39,7 @@ type connectionParams struct { dbname string host string port int + appname string retry *retry.Retry } @@ -93,7 +96,7 @@ func newConnectionParamsFromEnv() connectionParams { port: env.GetInt("POSTGRES_PORT"), // Retry connections by default - retry: &retry.Retry{MinWait: 2, MaxWait: 4, MaxRetries: 3}, + retry: &DefaultConnectRetry, } } @@ -129,6 +132,12 @@ func WithPort(port int) ConnectionOption { } } +func WithAppName(appName string) ConnectionOption { + return func(params *connectionParams) { + params.appname = appName + } +} + func WithRetries(r retry.Retry) ConnectionOption { return func(params *connectionParams) { params.retry = &r @@ -208,6 +217,10 @@ func NewPgxClient(opts ...ConnectionOption) *pgxpool.Pool { panic(err) } + if params.appname != "" { + config.ConnConfig.RuntimeParams["application_name"] = params.appname + } + config.ConnConfig.Logger = &pgxTracer{continueOnly: true} config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {