GORM store for Session
$ go get -u -v github.com/go-session/gorm
package main
import (
"context"
"fmt"
"net/http"
gormstore "github.com/go-session/gorm"
"github.com/go-session/session"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func main() {
store := gormstore.MustStore(gormstore.Config{}, "sqlite3", "session.db")
defer store.Close()
session.InitManager(
session.SetStore(store),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
store, err := session.Start(context.Background(), w, r)
if err != nil {
fmt.Fprint(w, err)
return
}
store.Set("foo", "bar")
err = store.Save()
if err != nil {
fmt.Fprint(w, err)
return
}
http.Redirect(w, r, "/foo", 302)
})
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
store, err := session.Start(context.Background(), w, r)
if err != nil {
fmt.Fprint(w, err)
return
}
foo, ok := store.Get("foo")
if ok {
fmt.Fprintf(w, "foo:%s", foo)
return
}
fmt.Fprint(w, "does not exist")
})
http.ListenAndServe(":8080", nil)
}
$ go build server.go
$ ./server
foo:bar
Copyright (c) 2019 Lyric