I'm confused on how to approach this.
It seems to be that GAE wants every client library to use a context.Context scoped to a http.Request.
I previously have experience doing something like this:
main.go
type server struct {
db *firestore.Client
}
func main() {
// Setup server
s := &server{db: NewFirestoreClient()}
// Setup Router
http.HandleFunc("/people", s.peopleHandler())
// Starts the server to receive requests
appengine.Main()
}
func (s *server) peopleHandler() http.HandlerFunc {
// pass context in this closure from main?
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() // appengine.NewContext(r) but should it inherit from background somehow?
s.person(ctx, 1)
// ...
}
}
func (s *server) person(ctx context.Context, id int) {
// what context should this be?
_, err := s.db.Client.Collection("people").Doc(uid).Set(ctx, p)
// handle client results
}
firebase.go
// Firestore returns a warapper for client
type Firestore struct {
Client *firestore.Client
}
// NewFirestoreClient returns a firestore struct client to use for firestore db access
func NewFirestoreClient() *Firestore {
ctx := context.Background()
client, err := firestore.NewClient(ctx, os.Getenv("GOOGLE_PROJECT_ID"))
if err != nil {
log.Fatal(err)
}
return &Firestore{
Client: client,
}
}
This has big implications on how to scope a project wide client. E.g hanging off of a server{db: client}
and attaching handlers on that struct or having to pass it off via dependency injection within the request.
I do notice that the calls out using the client require another context. So maybe it should be like:
-
main.go
create actx := context.Background()
-
main.go
pass that into new client handler ctx := appengine.NewContext(r)
Basically the initial setup doesn't matter off of context.Background()
because new requests have a different context from App Engine?
I could pass in ctx into the handler from main and then NewContext off of that + the request?
What's the idiomatic approach to this?
note: I had firestore methods off of the Firestore
struct as well in previous iterations...