Gorilla's Session store implementation with MongoDB
Forked from github.com/kidstuff/mongostore to migrate from mgo to mongo-go-driver.
Depends on the mongo-go-driver library.
go get github.com/ashulepov/mongodbstore
Available on godoc.org.
func foo(rw http.ResponseWriter, req *http.Request) {
// Context
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
// Fetch new store.
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
store := mongodbstore.NewMongoDBStore(client.Database("test").Collection("test_session"), 3600, true,
[]byte("secret-key"))
// Get a session.
session, err := store.Get(req, "session-key")
if err != nil {
log.Println(err.Error())
}
// Add a value.
session.Values["foo"] = "bar"
// Save.
if err = sessions.Save(req, rw); err != nil {
log.Printf("Error saving session: %v", err)
}
fmt.Fprintln(rw, "ok")
}