Session store using Bolt
Find a file
2014-06-18 11:50:40 +09:00
reaper Add examples 2014-06-18 11:50:40 +09:00
shared Add tests 2014-06-17 17:53:24 +09:00
store Add examples 2014-06-18 11:50:40 +09:00
.gitignore Update store/store_test.go 2014-06-18 00:19:04 +09:00
LICENSE Initial commit 2014-06-13 18:46:18 +09:00
Makefile Add a reaper 2014-06-15 01:15:10 +09:00
README.md Update README.md 2014-06-18 10:50:12 +09:00
wercker.yml Update wercker.yml 2014-06-17 16:56:36 +09:00

BoltStore - Session store using Bolt

wercker status Coverage Status GoDoc

About

BoltStore is a session store using Bolt. This store implements the gorilla/sessions package's Store interface.

Installation

go get github.com/yosssi/boltstore/...

Example

package main

import (
	"fmt"
	"net/http"

	"github.com/boltdb/bolt"
	"github.com/gorilla/sessions"
	"github.com/yosssi/boltstore/reaper"
	"github.com/yosssi/boltstore/store"
)

var db *bolt.DB

func handler(w http.ResponseWriter, r *http.Request) {
	// Fetch a new store.
	str, err := store.New(db, store.Config{}, []byte("secret-key"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	// Get a session.
	session, err := str.Get(r, "session-key")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	// Add a value on the session.
	session.Values["foo"] = "bar"

	// Save the session.
	if err := sessions.Save(r, w); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	// Delete the session.
	session.Options.MaxAge = -1
	if err := sessions.Save(r, w); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	fmt.Fprintf(w, "Hello BoltStore")
}

func main() {
	var err error
	// Open a Bolt database.
	db, err = bolt.Open("./sessions.db", 0666)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	// Invoke a reaper which checks and removes expired sessions periodically.
	defer reaper.Quit(reaper.Run(db, reaper.Options{}))
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

Documentation