Session middleware for Echo
$ go get -u -v github.com/go-session/echo-session
package main
import (
"fmt"
"net/http"
"github.com/go-session/echo-session"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.Use(echosession.New())
e.GET("/", func(ctx echo.Context) error {
store := echosession.FromContext(ctx)
store.Set("foo", "bar")
err := store.Save()
if err != nil {
return err
}
return ctx.Redirect(302, "/foo")
})
e.GET("/foo", func(ctx echo.Context) error {
store := echosession.FromContext(ctx)
foo, ok := store.Get("foo")
if !ok {
return ctx.String(http.StatusNotFound, "not found")
}
return ctx.String(http.StatusOK, fmt.Sprintf("foo:%s", foo))
})
e.Logger.Fatal(e.Start(":8080"))
}
$ go build server.go
$ ./server
foo:bar
Copyright (c) 2018 Lyric