mongostore/README.md

35 lines
862 B
Markdown
Raw Normal View History

2020-04-02 22:04:53 +02:00
```go
import (
"github.com/dalu/mongostore"
)
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("hellol-worlol"))
store.Options.Path = "/"
store.Options.MaxAge = 86400 * 30 * 365
defer store.Close()
// 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)
}
}
```