English | 中文
This is a middleware for hertz.
Hertz middleware for session management with multi-backend support:
This repo is forked from sessions and adapted for hertz.
Download and install it:
go get github.com/hertz-contrib/sessions
Import it in your code:
import "github.com/hertz-contrib/sessions"
package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/cookie" ) func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) h.Use(sessions.New("mysession", store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) if session.Get("hello") != "world" { session.Set("hello", "world") session.Save() } c.JSON(200, utils.H{"hello": session.Get("hello")}) }) h.Spin() }
package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/cookie" ) func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) sessionNames := []string{"a", "b"} h.Use(sessions.Many(sessionNames, store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { sessionA := sessions.DefaultMany(c, "a") sessionB := sessions.DefaultMany(c, "b") if sessionA.Get("hello") != "world!" { sessionA.Set("hello", "world!") sessionA.Save() } if sessionB.Get("hello") != "world?" { sessionB.Set("hello", "world?") sessionB.Save() } c.JSON(200, utils.H{ "a": sessionA.Get("hello"), "b": sessionB.Get("hello"), }) }) h.Spin() }
package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/cookie" ) func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int v := session.Get("count") if v == nil { count = 0 } else { count = v.(int) count++ } session.Set("count", count) session.Save() c.JSON(200, utils.H{"count": count}) }) h.Spin() }
package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/redis" ) func main() { h := server.Default(server.WithHostPorts(":8000")) store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret")) h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int v := session.Get("count") if v == nil { count = 0 } else { count = v.(int) count++ } session.Set("count", count) session.Save() c.JSON(200, utils.H{"count": count}) }) h.Spin() }
package main import ( "context" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/hertz-contrib/sessions" "github.com/hertz-contrib/sessions/rediscluster" ) func main() { h := server.Default(server.WithHostPorts(":8000")) store, _ := rediscluster.NewStore(10, []string{"localhost:5001", "localhost:5002"}, "", nil, []byte("secret")) h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int v := session.Get("count") if v == nil { count = 0 } else { count = v.(int) count++ } session.Set("count", count) session.Save() c.JSON(200, utils.H{"count": count}) }) h.Spin() }
This project is under Apache License. See the LICENSE file for the full license text.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4