From 10d2e77c4fa8f6e3ca8f87c203eb3b3f4a7237be Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 7 May 2022 15:04:35 -0400 Subject: [PATCH] use request context --- internal/api/accounts.go | 19 ++++++++++--------- internal/api/devices.go | 10 +++++----- internal/api/notifications.go | 5 ++--- internal/api/receipt.go | 3 +-- internal/api/watcher.go | 9 ++++----- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/internal/api/accounts.go b/internal/api/accounts.go index bc6a5b3..8a81204 100644 --- a/internal/api/accounts.go +++ b/internal/api/accounts.go @@ -21,6 +21,8 @@ type accountNotificationsRequest struct { } func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + anr := &accountNotificationsRequest{} if err := json.NewDecoder(r.Body).Decode(anr); err != nil { a.errorResponse(w, r, 500, err.Error()) @@ -31,8 +33,6 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request apns := vars["apns"] rid := vars["redditID"] - ctx := context.Background() - dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) if err != nil { a.errorResponse(w, r, 500, err.Error()) @@ -54,12 +54,12 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request } func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) apns := vars["apns"] rid := vars["redditID"] - ctx := context.Background() - dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) if err != nil { a.errorResponse(w, r, 500, err.Error()) @@ -85,12 +85,12 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ } func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) apns := vars["apns"] rid := vars["redditID"] - ctx := context.Background() - dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) if err != nil { a.errorResponse(w, r, 500, err.Error()) @@ -112,11 +112,11 @@ func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) } func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) apns := vars["apns"] - ctx := context.Background() - dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) if err != nil { a.errorResponse(w, r, 422, err.Error()) @@ -206,9 +206,10 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) - ctx := context.Background() var acct domain.Account if err := json.NewDecoder(r.Body).Decode(&acct); err != nil { diff --git a/internal/api/devices.go b/internal/api/devices.go index 3a963a4..5da9e9d 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -1,7 +1,6 @@ package api import ( - "context" "encoding/json" "fmt" "net/http" @@ -20,7 +19,7 @@ import ( const notificationTitle = "📣 Hello, is this thing on?" func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() d := &domain.Device{} if err := json.NewDecoder(r.Body).Decode(d); err != nil { @@ -40,9 +39,9 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) + ctx := r.Context() - ctx := context.Background() + vars := mux.Vars(r) tok := vars["apns"] d, err := a.deviceRepo.GetByAPNSToken(ctx, tok) @@ -92,8 +91,9 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) - ctx := context.Background() dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"]) if err != nil { diff --git a/internal/api/notifications.go b/internal/api/notifications.go index 36fb0b3..e92ef1f 100644 --- a/internal/api/notifications.go +++ b/internal/api/notifications.go @@ -1,7 +1,6 @@ package api import ( - "context" "fmt" "net/http" @@ -25,9 +24,9 @@ type notificationGenerator func(*payload.Payload) func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) + ctx := r.Context() - ctx := context.Background() + vars := mux.Vars(r) tok := vars["apns"] d, err := a.deviceRepo.GetByAPNSToken(ctx, tok) diff --git a/internal/api/receipt.go b/internal/api/receipt.go index 831e44c..903298a 100644 --- a/internal/api/receipt.go +++ b/internal/api/receipt.go @@ -1,7 +1,6 @@ package api import ( - "context" "encoding/json" "io/ioutil" "net/http" @@ -15,7 +14,7 @@ import ( ) func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() vars := mux.Vars(r) apns := vars["apns"] diff --git a/internal/api/watcher.go b/internal/api/watcher.go index 5edff5d..69f2f40 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -1,7 +1,6 @@ package api import ( - "context" "encoding/json" "net/http" "strconv" @@ -44,7 +43,7 @@ type watcherCreatedResponse struct { } func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() vars := mux.Vars(r) apns := vars["apns"] @@ -175,7 +174,7 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() vars := mux.Vars(r) id, err := strconv.ParseInt(vars["watcherID"], 10, 64) @@ -216,7 +215,7 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() vars := mux.Vars(r) id, err := strconv.ParseInt(vars["watcherID"], 10, 64) @@ -250,7 +249,7 @@ type watcherItem struct { } func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() + ctx := r.Context() vars := mux.Vars(r) apns := vars["apns"]