https://github.com/quasoft/memstore
In-memory store for gorilla/sessions for use in tests and dev environments
https://github.com/quasoft/memstore
go golang inmemory session-management sessions stub tests
Last synced: 4 months ago
JSON representation
In-memory store for gorilla/sessions for use in tests and dev environments
- Host: GitHub
- URL: https://github.com/quasoft/memstore
- Owner: quasoft
- License: bsd-3-clause
- Created: 2018-01-14T18:03:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-10T06:26:23.000Z (over 6 years ago)
- Last Synced: 2024-06-18T17:07:11.895Z (almost 2 years ago)
- Topics: go, golang, inmemory, session-management, sessions, stub, tests
- Language: Go
- Size: 17.6 KB
- Stars: 18
- Watchers: 2
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# memstore
[](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)
In-memory implementation of [gorilla/sessions](https://github.com/gorilla/sessions) for use in tests and dev environments
## How to install
go get github.com/quasoft/memstore
## Documentation
Documentation, as usual, can be found at [godoc.org](http://www.godoc.org/github.com/quasoft/memstore).
The interface of [gorilla/sessions](https://github.com/gorilla/sessions) is described at http://www.gorillatoolkit.org/pkg/sessions.
### How to use
``` go
package main
import (
"fmt"
"log"
"net/http"
"github.com/quasoft/memstore"
)
func main() {
// Create a memory store, providing authentication and
// encryption key for securecookie
store := memstore.NewMemStore(
[]byte("authkey123"),
[]byte("enckey12341234567890123456789012"),
)
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
// Get session by name.
session, err := store.Get(r, "session1")
if err != nil {
log.Printf("Error retrieving session: %v", err)
}
// The name should be 'foobar' if home page was visited before that and 'Guest' otherwise.
user, ok := session.Values["username"]
if !ok {
user = "Guest"
}
fmt.Fprintf(w, "Hello %s", user)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Get session by name.
session, err := store.Get(r, "session1")
if err != nil {
log.Printf("Error retrieving session: %v", err)
}
// Add values to the session object
session.Values["username"] = "foobar"
session.Values["email"] = "spam@eggs.com"
// Save values
err = session.Save(r, w)
if err != nil {
log.Fatalf("Error saving session: %v", err)
}
})
log.Printf("listening on http://%s/", "127.0.0.1:9090")
log.Fatal(http.ListenAndServe("127.0.0.1:9090", nil))
}
```