gorilla-sessions-mongodb
Gorilla's Session store implementation for mongoDB using official Go driver
Installation
go get github.com/2-72/gorilla-sessions-mongodb
Example
package main
import (
"context"
"log"
"net/http"
"os"
mongodbstore "github.com/2-72/gorilla-sessions-mongodb"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Printf("error connecting to mongodb %v \n", err)
return
}
coll := client.Database("app_db").Collection("sessions")
store, err := mongodbstore.NewMongoDBStore(coll, []byte(os.Getenv("SESSION_KEY")))
if err != nil {
log.Printf("error initializing mongodb store %v \n", err)
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
session.Values["foo"] = "bar"
session.Values[42] = 43
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}