# README
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) {
// Get a session. We're ignoring the error resulted from decoding an
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session-name")
// Set some session values.
session.Values["foo"] = "bar"
session.Values[42] = 43
// Save it before we write to the response/return from the handler.
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
# Functions
NewMongoDBStore returns a new NewMongoDBStore with default config
defaultConfig := MongoDBStoreConfig{ IndexTTL: true, SessionOptions: sessions.Options{ Path: "/", MaxAge: 3600 * 24 * 30, HttpOnly: true, }, }.
NewMongoDBStoreWithConfig returns a new NewMongoDBStore with a custom MongoDBStoreConfig.
# Structs
MongoDBStore stores sessions using mongoDB as backend.
MongoDBStoreConfig is a configuration options for MongoDBStore.