session
Session
原文:https://echo.labstack.com/docs/middleware/session
Session middleware facilitates HTTP session management backed by gorilla sessions. The default implementation provides cookie and filesystem based session store; however, you can take advantage of community maintained implementation for various backends.
NOTE
Echo community contribution
Dependencies
1
2
3
4
| import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
)
|
Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
e.GET("/", func(c echo.Context) error {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
sess.Values["foo"] = "bar"
sess.Save(c.Request(), c.Response())
return c.NoContent(http.StatusOK)
})
|
Custom Configuration
Usage
1
2
| e := echo.New()
e.Use(session.MiddlewareWithConfig(session.Config{}))
|
Configuration
1
2
3
4
5
6
7
8
| Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Session store.
// Required.
Store sessions.Store
}
|
Default Configuration
1
2
3
| DefaultConfig = Config{
Skipper: DefaultSkipper,
}
|