From 96b18f6f284106f493ca5db776a005006ac77dfa Mon Sep 17 00:00:00 2001 From: Jens-Uwe Mager Date: Fri, 26 Nov 2021 17:17:01 +0100 Subject: [PATCH] Add documentation on how use firestore. --- firestore/README.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/firestore/README.md b/firestore/README.md index 5c396e0..c5266d5 100644 --- a/firestore/README.md +++ b/firestore/README.md @@ -4,7 +4,9 @@ A [Google Cloud Firestore](https://pkg.go.dev/cloud.google.com/go/firestore) bas ## Setup -You should follow the instructions to [install and open a database](https://cloud.google.com/firestore/docs), and pass the database to `firestore.New()` to establish the session store. +You should follow the instructions to [install and open a database](https://cloud.google.com/firestore/docs), and pass the database to `firestore.New()` to establish the session store. + +The default collection is "Sessions". If you want to change that, store a custom CollectionRef in scsfs.Sessions. ## Example @@ -12,22 +14,29 @@ You should follow the instructions to [install and open a database](https://clou package main import ( + "context" "io" + "log" "net/http" + "os" + "cloud.google.com/go/firestore" + scsfs "github.com/alexedwards/scs/firestore" "github.com/alexedwards/scs/v2" - "github.com/alexedwards/scs/firestore" ) var sessionManager *scs.SessionManager func main() { // Establish connection to Google Cloud Firestore. - // ... - + db, err := firestore.NewClient(context.Background(), os.Getenv("GOOGLE_CLOUD_PROJECT")) + if err != nil { + log.Fatal(err) + } + // Initialize a new session manager and configure it to use firestore as the session store. sessionManager = scs.New() - sessionManager.Store = firestore.New(db) + sessionManager.Store = scsfs.New(db) mux := http.NewServeMux() mux.HandleFunc("/put", putHandler) @@ -45,3 +54,17 @@ func getHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, msg) } ``` + +The sample can be run by setting the environment variable GOOGLE_CLOUD_PROJECT, which should be proper project id. If you have the local firestore emulator (part of the Google Cloud SDK) installed you can start a local emulator and run the example like this: + +Start the emulator first: + +```sh +gcloud beta emulators firestore start --host-port=localhost:8041 +``` + +Then test the example program: + +```sh +FIRESTORE_EMULATOR_HOST=localhost:8041 GOOGLE_CLOUD_PROJECT=test go run . +``` \ No newline at end of file