A PostgreSQL store for gorilla/sessions - src.
go get github.com/philboltt/postgrestore
Available on godoc.org.
See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.
// Fetch new store (path="/", MaxAge=30days, i.e. 60sec*60min*24hrs*30days)
store := NewPostgreSQLStore("postgres://user:password@server:port/database?sslmode=disable", "/", 60*60*24*30, []byte("secret-key"))
defer store.Close()
// Get a session.
session, err = store.Get(req, "mySessionName")
if err != nil {
log.Error(err.Error())
}
// Add a value.
session.Values["foo"] = "bar"
// Save - creates a new record in the database if none exists, or updates an existing one.
if err = sessions.Save(req, rsp); err != nil {
t.Fatalf("Error saving session: %v", err)
}
// Delete - removes session record from the database and clears the session ID from the client cookie.
store.Delete(resp, session)
See the tests for more examples.
This package is largely a rehash of the code and ideas from these fine repos:
Thank you all for sharing your code!