Proper context.Context usage

This commit is contained in:
Caleb Gardner
2024-10-24 00:00:08 -05:00
parent fcab9458ee
commit 6965917e76
19 changed files with 126 additions and 118 deletions
+2 -2
View File
@@ -49,8 +49,8 @@ func (s *SWBackend) AddBackend(b *backend.Backend) {
s.back = b
}
func (s *SWBackend) AddCrash(cr backend.IndividualCrash) bool {
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
func (s *SWBackend) ShouldAddCrash(ctx context.Context, cr backend.IndividualCrash) bool {
res := s.db.Collection("versions").FindOne(ctx, bson.M{"version": cr.Version})
return res.Err() != mongo.ErrNoDocuments
}
+2 -3
View File
@@ -1,7 +1,6 @@
package swassistant
import (
"context"
"encoding/json"
"io"
"log"
@@ -61,7 +60,7 @@ func (s *SWBackend) UploadProfile(w http.ResponseWriter, r *http.Request) {
Type: profType,
Profile: prof,
}
_, err = s.db.Collection("profiles").InsertOne(context.Background(), toUpload)
_, err = s.db.Collection("profiles").InsertOne(r.Context(), toUpload)
if err != nil {
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
log.Println("error inserting profile:", err)
@@ -72,7 +71,7 @@ func (s *SWBackend) UploadProfile(w http.ResponseWriter, r *http.Request) {
}
func (s *SWBackend) GetProfile(w http.ResponseWriter, r *http.Request) {
res := s.db.Collection("profiles").FindOne(context.Background(), bson.M{"_id": r.PathValue("profileID")})
res := s.db.Collection("profiles").FindOne(r.Context(), bson.M{"_id": r.PathValue("profileID")})
if res.Err() == mongo.ErrNoDocuments {
backend.ReturnError(w, 404, "not found", "Profile not found")
return
+5 -5
View File
@@ -1,7 +1,6 @@
package swassistant
import (
"context"
"encoding/json"
"log"
"net/http"
@@ -30,7 +29,8 @@ func (s *SWBackend) ListRooms(w http.ResponseWriter, r *http.Request) {
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
return
}
res, err := s.db.Collection("rooms").Find(context.Background(), bson.M{"users": hdr.User.Username}, options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "owner": 1}))
res, err := s.db.Collection("rooms").Find(r.Context(), bson.M{"users": hdr.User.Username},
options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "owner": 1}))
if err != nil && err != mongo.ErrNoDocuments {
log.Println("error getting room list:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
@@ -42,7 +42,7 @@ func (s *SWBackend) ListRooms(w http.ResponseWriter, r *http.Request) {
Owner string `json:"owner" bson:"owner"`
}, 0)
if err == nil {
err = res.All(context.Background(), &out)
err = res.All(r.Context(), &out)
if err != nil {
log.Println("error decoding room list:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
@@ -74,7 +74,7 @@ func (s *SWBackend) NewRoom(w http.ResponseWriter, r *http.Request) {
Users: []string{},
Profiles: []string{},
}
_, err = s.db.Collection("rooms").InsertOne(context.Background(), newRoom)
_, err = s.db.Collection("rooms").InsertOne(r.Context(), newRoom)
if err != nil {
log.Println("error creating room:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
@@ -93,7 +93,7 @@ func (s *SWBackend) GetRoom(w http.ResponseWriter, r *http.Request) {
return
}
roomID := r.PathValue("roomID")
res := s.db.Collection("rooms").FindOne(context.TODO(), bson.M{"_id": roomID})
res := s.db.Collection("rooms").FindOne(r.Context(), bson.M{"_id": roomID})
if res.Err() == mongo.ErrNoDocuments {
backend.ReturnError(w, http.StatusNotFound, "not found", "Room not found")
return