2014-01-07 17:15:27 +00:00
|
|
|
# pgstore
|
|
|
|
|
|
|
|
A session store backend for [gorilla/sessions](http://www.gorillatoolkit.org/pkg/sessions) - [src](https://github.com/gorilla/sessions).
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
make get-deps
|
|
|
|
|
|
|
|
## Documentation
|
|
|
|
|
2014-10-11 21:17:54 +00:00
|
|
|
Available on [godoc.org](http://www.godoc.org/github.com/antonlindstrom/pgstore).
|
2014-01-07 17:15:27 +00:00
|
|
|
|
|
|
|
See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
2016-07-21 20:34:24 +00:00
|
|
|
[embedmd]:# (examples/sessions.go)
|
2015-09-13 19:14:50 +00:00
|
|
|
```go
|
2016-07-21 20:34:24 +00:00
|
|
|
package examples
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/antonlindstrom/pgstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExampleHandler is an example that displays the usage of PGStore.
|
|
|
|
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Fetch new store.
|
|
|
|
store, err := pgstore.NewPGStore("postgres://user:password@127.0.0.1:5432/database?sslmode=verify-full", []byte("secret-key"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
// Run a background goroutine to clean up expired sessions from the database.
|
|
|
|
defer store.StopCleanup(store.Cleanup(time.Minute * 5))
|
|
|
|
|
|
|
|
// Get a session.
|
|
|
|
session, err := store.Get(r, "session-key")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a value.
|
|
|
|
session.Values["foo"] = "bar"
|
|
|
|
|
|
|
|
// Save.
|
|
|
|
if err = session.Save(r, w); err != nil {
|
|
|
|
log.Fatalf("Error saving session: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete session.
|
|
|
|
session.Options.MaxAge = -1
|
|
|
|
if err = session.Save(r, w); err != nil {
|
|
|
|
log.Fatalf("Error saving session: %v", err)
|
|
|
|
}
|
2015-09-13 19:14:50 +00:00
|
|
|
}
|
|
|
|
```
|
2014-01-07 17:15:27 +00:00
|
|
|
|
2016-07-19 19:21:08 +00:00
|
|
|
## Breaking changes
|
|
|
|
|
|
|
|
* 2016-07-19 - `NewPGStore` and `NewPGStoreFromPool` now returns `(*PGStore, error)`
|
|
|
|
|
2014-01-07 17:15:27 +00:00
|
|
|
## Thanks
|
|
|
|
|
|
|
|
I've stolen, borrowed and gotten inspiration from the other backends available:
|
|
|
|
|
|
|
|
* [redistore](https://github.com/boj/redistore)
|
|
|
|
* [mysqlstore](https://github.com/srinathgs/mysqlstore)
|
|
|
|
* [babou dbstore](https://github.com/drbawb/babou/blob/master/lib/session/dbstore.go)
|
|
|
|
|
|
|
|
Thank you all for sharing your code!
|
|
|
|
|
|
|
|
What makes this backend different is that it's for Postgresql and uses the fine
|
|
|
|
datamapper [Gorp](https://github.com/coopernurse/gorp).
|
|
|
|
Make sure you use a somewhat new codebase of Gorp as it now defaults to text for
|
|
|
|
strings when it used to default to varchar 255. Varchar 255 is unfortunately too
|
|
|
|
small.
|