README.md (2296B)
1 # memstore 2 3 [](https://godoc.org/github.com/quasoft/memstore) [](https://travis-ci.org/quasoft/memstore) [](https://coveralls.io/github/quasoft/memstore?branch=master) [](https://goreportcard.com/report/github.com/quasoft/memstore) 4 5 In-memory implementation of [gorilla/sessions](https://github.com/gorilla/sessions) for use in tests and dev environments 6 7 ## How to install 8 9 go get github.com/quasoft/memstore 10 11 ## Documentation 12 13 Documentation, as usual, can be found at [godoc.org](http://www.godoc.org/github.com/quasoft/memstore). 14 15 The interface of [gorilla/sessions](https://github.com/gorilla/sessions) is described at http://www.gorillatoolkit.org/pkg/sessions. 16 17 ### How to use 18 ``` go 19 package main 20 21 import ( 22 "fmt" 23 "log" 24 "net/http" 25 26 "github.com/quasoft/memstore" 27 ) 28 29 func main() { 30 // Create a memory store, providing authentication and 31 // encryption key for securecookie 32 store := memstore.NewMemStore( 33 []byte("authkey123"), 34 []byte("enckey12341234567890123456789012"), 35 ) 36 37 http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { 38 // Get session by name. 39 session, err := store.Get(r, "session1") 40 if err != nil { 41 log.Printf("Error retrieving session: %v", err) 42 } 43 44 // The name should be 'foobar' if home page was visited before that and 'Guest' otherwise. 45 user, ok := session.Values["username"] 46 if !ok { 47 user = "Guest" 48 } 49 fmt.Fprintf(w, "Hello %s", user) 50 }) 51 52 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 53 // Get session by name. 54 session, err := store.Get(r, "session1") 55 if err != nil { 56 log.Printf("Error retrieving session: %v", err) 57 } 58 59 // Add values to the session object 60 session.Values["username"] = "foobar" 61 session.Values["email"] = "spam@eggs.com" 62 63 // Save values 64 err = session.Save(r, w) 65 if err != nil { 66 log.Fatalf("Error saving session: %v", err) 67 } 68 }) 69 70 log.Printf("listening on http://%s/", "127.0.0.1:9090") 71 log.Fatal(http.ListenAndServe("127.0.0.1:9090", nil)) 72 } 73 74 ```