...
1# pgstore
2
3A session store backend for [gorilla/sessions](http://www.gorillatoolkit.org/pkg/sessions) - [src](https://github.com/gorilla/sessions).
4
5## Installation
6
7 make get-deps
8
9## Documentation
10
11Available on [godoc.org](http://www.godoc.org/github.com/antonlindstrom/pgstore).
12
13See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.
14
15### Example
16
17[embedmd]:# (examples/sessions.go)
18```go
19package examples
20
21import (
22 "log"
23 "net/http"
24 "time"
25
26 "github.com/antonlindstrom/pgstore"
27)
28
29// ExampleHandler is an example that displays the usage of PGStore.
30func ExampleHandler(w http.ResponseWriter, r *http.Request) {
31 // Fetch new store.
32 store, err := pgstore.NewPGStore("postgres://user:password@127.0.0.1:5432/database?sslmode=verify-full", []byte("secret-key"))
33 if err != nil {
34 log.Fatalf(err.Error())
35 }
36 defer store.Close()
37
38 // Run a background goroutine to clean up expired sessions from the database.
39 defer store.StopCleanup(store.Cleanup(time.Minute * 5))
40
41 // Get a session.
42 session, err := store.Get(r, "session-key")
43 if err != nil {
44 log.Fatalf(err.Error())
45 }
46
47 // Add a value.
48 session.Values["foo"] = "bar"
49
50 // Save.
51 if err = session.Save(r, w); err != nil {
52 log.Fatalf("Error saving session: %v", err)
53 }
54
55 // Delete session.
56 session.Options.MaxAge = -1
57 if err = session.Save(r, w); err != nil {
58 log.Fatalf("Error saving session: %v", err)
59 }
60}
61```
62
63## Breaking changes
64
65* 2016-07-19 - `NewPGStore` and `NewPGStoreFromPool` now returns `(*PGStore, error)`
66
67## Thanks
68
69I've stolen, borrowed and gotten inspiration from the other backends available:
70
71* [redistore](https://github.com/boj/redistore)
72* [mysqlstore](https://github.com/srinathgs/mysqlstore)
73* [babou dbstore](https://github.com/drbawb/babou/blob/master/lib/session/dbstore.go)
74
75Thank you all for sharing your code!
76
77What makes this backend different is that it's for PostgreSQL.
78
79We've recently refactored this backend to use the standard database/sql driver instead of Gorp. This removes a dependency and makes this package very lightweight and makes database interactions very transparent. Lastly, from the standpoint of unit testing where you want to mock the database layer instead of requiring a real database, you can now easily use a package like [go-SQLMock](https://github.com/DATA-DOG/go-sqlmock) to do just that.
View as plain text