aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 28c2235f64ea017c4d8cc8dc48f1387febefe53a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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