Update README.md

This commit is contained in:
Keiji Yoshida 2014-06-15 01:53:34 +09:00
parent 61e551bfab
commit 12c03ff460

View file

@ -15,22 +15,38 @@ go get github.com/yosssi/boltstore/...
## Example ## Example
```go ```go
// Fetch new store. package main
store, err := boltstore.New(
boltstore.Config{ 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) {
if r.URL.Path[1:] == "favicon.ico" {
return
}
// Fetch a new store.
str, err := store.New(
db,
store.Config{
SessionOptions: sessions.Options{ SessionOptions: sessions.Options{
MaxAge: 60 * 60 * 24 * 30, // 30days MaxAge: 60 * 60 * 24 * 30, // 30days
}, },
}, },
[]byte("secret-key"), []byte("secret-key"),
) )
if err != nil {
panic(err)
}
defer store.Close()
// Get a session. // Get a session.
session, err := store.Get(r, "session-key") session, err := str.Get(r, "session-key")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -48,6 +64,21 @@ session.Options.MaxAge = -1
if err := sessions.Save(r, w); err != nil { if err := sessions.Save(r, w); err != nil {
panic(err) panic(err)
} }
fmt.Fprintf(w, "Hello BoltStore")
}
func main() {
db, err := bolt.Open("./sessions.db", 0666)
if err != nil {
panic(err)
}
defer db.Close()
// Invoke a reaper which removes expired sessions.
defer reaper.Quit(reaper.Run(db, reaper.Options{}))
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
``` ```
## Documentation ## Documentation