Session middleware for Gear
$ go get -u -v github.com/go-session/gear-session
package main
import (
"fmt"
"net/http"
"github.com/go-session/gear-session"
"github.com/teambition/gear"
)
func main() {
app := gear.New()
app.Use(gearsession.New())
router := gear.NewRouter()
router.Get("/", func(ctx *gear.Context) error {
store := gearsession.FromContext(ctx)
store.Set("foo", "bar")
err := store.Save()
if err != nil {
return gear.ErrInternalServerError.From(err)
}
return ctx.Redirect("/foo")
})
router.Get("/foo", func(ctx *gear.Context) error {
store := gearsession.FromContext(ctx)
foo, ok := store.Get("foo")
if !ok {
return gear.ErrNotFound
}
return ctx.End(http.StatusOK, []byte(fmt.Sprintf("foo:%s", foo)))
})
app.UseHandler(router)
app.Listen(":8080")
}
$ go build server.go
$ ./server
foo:bar
Copyright (c) 2018 Lyric