You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BoltStore is a session store using Bolt which is a pure Go key/value store. You can store session data in Bolt by using this store. This store implements the gorilla/sessions package's Store interface. BoltStore's APIs and examples can be seen on its GoDoc page.
Installation
gogetgithub.com/yosssi/boltstore/...
Example
Here is a simple example using BoltStore. You can see other examples on the BoltStore's GoDoc page.
package main
import (
"fmt""net/http""github.com/boltdb/bolt""github.com/gorilla/sessions""github.com/yosssi/boltstore/reaper""github.com/yosssi/boltstore/store"
)
vardb*bolt.DBfunchandler(w http.ResponseWriter, r*http.Request) {
// Create a store.str, err:=store.New(db, store.Config{}, []byte("secret-key"))
iferr!=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// Get a session.session, err:=str.Get(r, "session-key")
iferr!=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// Add a value on the session.session.Values["foo"] ="bar"// Save the session.iferr:=sessions.Save(r, w); err!=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintf(w, "Hello BoltStore")
}
funcmain() {
varerrerror// Open a Bolt database.db, err=bolt.Open("./sessions.db", 0666, nil)
iferr!=nil {
panic(err)
}
deferdb.Close()
// Invoke a reaper which checks and removes expired sessions periodically.deferreaper.Quit(reaper.Run(db, reaper.Options{}))
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}