Session middleware for Beego
$ go get -u -v github.com/go-session/beego-session
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/go-session/beego-session"
)
func main() {
app := beego.NewApp()
app.Handlers.InsertFilter("*", beego.BeforeRouter, beegosession.New())
app.Handlers.Get("/", func(ctx *context.Context) {
store := beegosession.FromContext(ctx)
store.Set("foo", "bar")
err := store.Save()
if err != nil {
ctx.Abort(500, err.Error())
return
}
ctx.Redirect(302, "/foo")
})
app.Handlers.Get("/foo", func(ctx *context.Context) {
store := beegosession.FromContext(ctx)
foo, ok := store.Get("foo")
if !ok {
ctx.Abort(404, "not found")
return
}
ctx.WriteString(fmt.Sprintf("foo:%s", foo))
})
beego.BConfig.Listen.HTTPPort = 8080
app.Run()
}
$ go build server.go
$ ./server
foo:bar
Copyright (c) 2018 Lyric