1
0
mirror of https://github.com/alexedwards/scs.git synced 2025-07-15 01:04:36 +02:00

Add documentation on how use firestore.

This commit is contained in:
Jens-Uwe Mager
2021-11-26 17:17:01 +01:00
parent dba928e4fe
commit 96b18f6f28

View File

@ -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 .
```