Collection of various Gorilla Sessions back-end stores.
go get github.com/bh90210/vaGorillaSessionsStores
Errors are excluded for brevity.
note: Badger will not work in distributed environments. Use it for local testing or single server scenarios.
import stores "github.com/bh90210/vagorillasessionsstores"
store, _ := stores.NewBadgerStore("/path/to/data", []byte(os.Getenv("SESSION_KEY")))
If path
is empty data will be stored in system's tmp
directory.
Start a store with custom options (see Badger's docs for more):
import stores "github.com/bh90210/vagorillasessionsstores"
opts := badger.Options{
Dir: "/data/dir",
}
store, _ := stores.NewBadgerStoreWithOpts(opts,[]byte(os.Getenv("SESSION_KEY")))
Two helper functions for direct back-end session manipulation without http request.
Starting a store entails passing credentials and client options (official go mongo driver is necessary):
import (
stores "github.com/bh90210/vagorillasessionsstores"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var cred options.Credential
cred.AuthSource = "YourAuthSource"
cred.Username = "YourUserName"
cred.Password = "YourPassword"
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI")).SetAuth(cred)
ctx := context.Background()
client, _ := mongo.Connect(ctx, opts)
store, _ := stores.NewMongoStore(client, "databaseName", "collectionName", []byte(os.Getenv("SESSION_KEY")))
If 'databaseName' & 'collectionName' are left empty the defaults are used ('sessions' & 'store').
store uses dgo/v200
Assumed schema:
sessionid: string @index(hash) .
sessionvalue: string .
type Session {
sessionid
sessionvalue
}
import (
stores "github.com/bh90210/vagorillasessionsstores"
"google.golang.org/grpc"
)
conn, _err_ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
store, _ := stores.NewDgraphStore(conn, []byte(os.Getenv("SESSION_KEY")))
You can also let schema initiation to the store:
import (
stores "github.com/bh90210/vagorillasessionsstores"
"google.golang.org/grpc"
)
conn, _err_ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
store, _ := stores.NewDgraphStoreWithSchema(conn, []byte(os.Getenv("SESSION_KEY")))