diff options
author | lyric <tiannianshou@gmail.com> | 2018-05-17 05:13:22 +0300 |
---|---|---|
committer | lyric <tiannianshou@gmail.com> | 2018-05-17 05:13:22 +0300 |
commit | 7c7e18faddc54ea614f135fcef8bc05649fa9452 (patch) | |
tree | e726fde3ee6835973b4ce9224dc16abdc75bc902 /README.md | |
parent | 23a9229ef11190057188f4615a519ca6c9f693c4 (diff) |
add echo middleware
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 83 |
1 files changed, 81 insertions, 2 deletions
@@ -1,2 +1,81 @@ -# echo-session -Session middleware for Echo. +# Session middleware for [Echo](https://github.com/labstack/echo) + +[![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url] + +## Quick Start + +### Download and install + +```bash +$ go get -u -v github.com/go-session/echo-session +``` + +### Create file `server.go` + +```go +package main + +import ( + "fmt" + "net/http" + + "github.com/go-session/echo-session" + "github.com/labstack/echo" + "gopkg.in/session.v2" +) + +func main() { + e := echo.New() + + e.Use(echosession.New( + session.SetCookieName("session_id"), + session.SetSign([]byte("sign")), + )) + + 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")) +} +``` + +### Build and run + +```bash +$ go build server.go +$ ./server +``` + +### Open in your web browser + +<http://localhost:8080> + + foo:bar + + +## MIT License + + Copyright (c) 2018 Lyric + +[reportcard-url]: https://goreportcard.com/report/github.com/go-session/echo-session +[reportcard-image]: https://goreportcard.com/badge/github.com/go-session/echo-session +[godoc-url]: https://godoc.org/github.com/go-session/echo-session +[godoc-image]: https://godoc.org/github.com/go-session/echo-session?status.svg +[license-url]: http://opensource.org/licenses/MIT +[license-image]: https://img.shields.io/npm/l/express.svg
\ No newline at end of file |