From 1b74c803da4b8da7bc5cebbd5c08b743602d66fd Mon Sep 17 00:00:00 2001 From: Vivek R Date: Fri, 31 May 2024 15:14:09 +0530 Subject: [PATCH] fix: update examples to v3 --- examples/fastglue-goredis/go.mod | 13 ---- examples/fastglue-redis/go.mod | 30 +++++++++ .../main.go | 28 ++++---- examples/fasthttp-inmemory/go.mod | 16 ++++- examples/fasthttp-inmemory/main.go | 27 ++++---- examples/fasthttp-redis/go.mod | 19 ++++-- examples/fasthttp-redis/go.mod.bak | 23 +++++++ examples/fasthttp-redis/main.go | 67 ++++++++++--------- examples/nethttp-inmemory/go.mod | 11 ++- examples/nethttp-inmemory/main.go | 27 ++++---- examples/nethttp-redis/go.mod | 18 +++-- examples/nethttp-redis/main.go | 67 +++++++++---------- examples/nethttp-secure-cookie/go.mod | 11 ++- examples/nethttp-secure-cookie/main.go | 30 ++++++--- go.work | 4 +- stores/postgres/go.mod | 8 ++- 16 files changed, 245 insertions(+), 154 deletions(-) delete mode 100644 examples/fastglue-goredis/go.mod create mode 100644 examples/fastglue-redis/go.mod rename examples/{fastglue-goredis => fastglue-redis}/main.go (83%) create mode 100644 examples/fasthttp-redis/go.mod.bak diff --git a/examples/fastglue-goredis/go.mod b/examples/fastglue-goredis/go.mod deleted file mode 100644 index aeb2984..0000000 --- a/examples/fastglue-goredis/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module github.com/vividvilla/simplesessions/examples/fastglue-goredis - -go 1.16 - -require ( - github.com/fasthttp/router v1.5.0 // indirect - github.com/klauspost/compress v1.17.8 // indirect - github.com/redis/go-redis/v9 v9.5.1 - github.com/valyala/fasthttp v1.52.0 - github.com/vividvilla/simplesessions/stores/goredis/v9 v9.0.0 - github.com/vividvilla/simplesessions/v3 v2.0.0 - github.com/zerodha/fastglue v1.8.0 -) diff --git a/examples/fastglue-redis/go.mod b/examples/fastglue-redis/go.mod new file mode 100644 index 0000000..e2d2eb7 --- /dev/null +++ b/examples/fastglue-redis/go.mod @@ -0,0 +1,30 @@ +module github.com/vividvilla/simplesessions/examples/fastglue-redis + +go 1.18 + +require ( + github.com/redis/go-redis/v9 v9.5.1 + github.com/valyala/fasthttp v1.54.0 + github.com/vividvilla/simplesessions/stores/redis/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 + github.com/zerodha/fastglue v1.8.0 +) + +require ( + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fasthttp/router v1.5.0 // indirect + github.com/klauspost/compress v1.17.8 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect + github.com/stretchr/testify v1.9.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace ( + github.com/vividvilla/simplesessions/stores/redis/v3 => ../../stores/redis + github.com/vividvilla/simplesessions/v3 => ../.. +) diff --git a/examples/fastglue-goredis/main.go b/examples/fastglue-redis/main.go similarity index 83% rename from examples/fastglue-goredis/main.go rename to examples/fastglue-redis/main.go index 1f96f85..2466528 100644 --- a/examples/fastglue-goredis/main.go +++ b/examples/fastglue-redis/main.go @@ -8,7 +8,7 @@ import ( "github.com/redis/go-redis/v9" "github.com/valyala/fasthttp" - redisstore "github.com/vividvilla/simplesessions/stores/goredis/v9" + redisstore "github.com/vividvilla/simplesessions/stores/redis/v3" "github.com/vividvilla/simplesessions/v3" "github.com/zerodha/fastglue" ) @@ -18,9 +18,9 @@ const ( ) var ( - sessionManager *simplesessions.Manager - testKey = "question" - testValue = 42 + sessMgr *simplesessions.Manager + testKey = "question" + testValue = 42 ) func initRedisGo(address, password string) *redis.Client { @@ -43,8 +43,11 @@ func initServer(name string, timeout int) *fasthttp.Server { } func setHandler(r *fastglue.Request) error { - - sess, err := sessionManager.Acquire(r.RequestCtx, r.RequestCtx, nil) + sess, err := sessMgr.Acquire(nil, r.RequestCtx, r.RequestCtx) + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(r.RequestCtx, r.RequestCtx) + } if err != nil { return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, GeneralError) } @@ -54,15 +57,11 @@ func setHandler(r *fastglue.Request) error { return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, GeneralError) } - if err = sess.Commit(); err != nil { - return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, GeneralError) - } - return r.SendEnvelope("success") } func getHandler(r *fastglue.Request) error { - sess, err := sessionManager.Acquire(r.RequestCtx, r.RequestCtx, nil) + sess, err := sessMgr.Acquire(nil, r.RequestCtx, r.RequestCtx) if err != nil { return r.SendErrorEnvelope(fasthttp.StatusInternalServerError, err.Error(), nil, GeneralError) } @@ -128,10 +127,9 @@ func main() { ctx := context.Background() store := redisstore.New(ctx, rc) - sessionManager = simplesessions.New(simplesessions.Options{}) - sessionManager.UseStore(store) - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + sessMgr.UseStore(store) + sessMgr.SetCookieHooks(getCookie, setCookie) g := fastglue.New() g.GET("/get", getHandler) diff --git a/examples/fasthttp-inmemory/go.mod b/examples/fasthttp-inmemory/go.mod index e66abb4..fb02acf 100644 --- a/examples/fasthttp-inmemory/go.mod +++ b/examples/fasthttp-inmemory/go.mod @@ -1,10 +1,20 @@ module github.com/vividvilla/simplesessions/examples/fasthttp-inmemory +go 1.18 + +require ( + github.com/valyala/fasthttp v0.0.0-20180901052036-d7688109a57b + github.com/vividvilla/simplesessions/stores/memory/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 +) + require ( github.com/klauspost/compress v1.4.0 // indirect github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e // indirect github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a // indirect - github.com/valyala/fasthttp v0.0.0-20180901052036-d7688109a57b - github.com/vividvilla/simplesessions v0.0.1 - github.com/vividvilla/simplesessions/stores/memory v0.0.0-20180905073812-64bb2453ba8a +) + +replace ( + github.com/vividvilla/simplesessions/stores/memory/v3 => ../../stores/memory + github.com/vividvilla/simplesessions/v3 => ../.. ) diff --git a/examples/fasthttp-inmemory/main.go b/examples/fasthttp-inmemory/main.go index c7293d9..ba50f17 100644 --- a/examples/fasthttp-inmemory/main.go +++ b/examples/fasthttp-inmemory/main.go @@ -5,31 +5,31 @@ import ( "net/http" "github.com/valyala/fasthttp" - "github.com/vividvilla/simplesessions" - "github.com/vividvilla/simplesessions/stores/memory" + "github.com/vividvilla/simplesessions/stores/memory/v3" + "github.com/vividvilla/simplesessions/v3" ) var ( - sessionManager *simplesessions.Manager + sessMgr *simplesessions.Manager testKey = "abc123" testValue = 123456 ) func setHandler(ctx *fasthttp.RequestCtx) { - sess, err := sessionManager.Acquire(ctx, ctx, nil) - if err != nil { - ctx.Error(err.Error(), 500) - return + sess, err := sessMgr.Acquire(nil, ctx, ctx) + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(ctx, ctx) } - err = sess.Set(testKey, testValue) if err != nil { ctx.Error(err.Error(), 500) return } - if err = sess.Commit(); err != nil { + err = sess.Set(testKey, testValue) + if err != nil { ctx.Error(err.Error(), 500) return } @@ -38,7 +38,7 @@ func setHandler(ctx *fasthttp.RequestCtx) { } func getHandler(ctx *fasthttp.RequestCtx) { - sess, err := sessionManager.Acquire(ctx, ctx, nil) + sess, err := sessMgr.Acquire(nil, ctx, ctx) if err != nil { ctx.Error(err.Error(), 500) return @@ -99,10 +99,9 @@ func setCookie(cookie *http.Cookie, w interface{}) error { } func main() { - sessionManager = simplesessions.New(simplesessions.Options{}) - sessionManager.UseStore(memory.New()) - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + sessMgr.UseStore(memory.New()) + sessMgr.SetCookieHooks(getCookie, setCookie) m := func(ctx *fasthttp.RequestCtx) { switch string(ctx.Path()) { diff --git a/examples/fasthttp-redis/go.mod b/examples/fasthttp-redis/go.mod index 384a419..d3e9678 100644 --- a/examples/fasthttp-redis/go.mod +++ b/examples/fasthttp-redis/go.mod @@ -1,16 +1,23 @@ module github.com/vividvilla/simplesessions/examples/fasthttp-redis -go 1.14 +go 1.18 require ( - github.com/gomodule/redigo v2.0.0+incompatible - github.com/valyala/fasthttp v1.52.0 - github.com/vividvilla/simplesessions/stores/redis/v2 v2.0.0 - github.com/vividvilla/simplesessions/v3 v2.0.0 + github.com/redis/go-redis/v9 v9.5.1 + github.com/valyala/fasthttp v1.54.0 + github.com/vividvilla/simplesessions/stores/redis/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 ) require ( github.com/andybalholm/brotli v1.1.0 // indirect - github.com/klauspost/compress v1.17.6 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/klauspost/compress v1.17.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect ) + +replace ( + github.com/vividvilla/simplesessions/stores/redis/v3 => ../../stores/redis + github.com/vividvilla/simplesessions/v3 => ../.. +) diff --git a/examples/fasthttp-redis/go.mod.bak b/examples/fasthttp-redis/go.mod.bak new file mode 100644 index 0000000..d3e9678 --- /dev/null +++ b/examples/fasthttp-redis/go.mod.bak @@ -0,0 +1,23 @@ +module github.com/vividvilla/simplesessions/examples/fasthttp-redis + +go 1.18 + +require ( + github.com/redis/go-redis/v9 v9.5.1 + github.com/valyala/fasthttp v1.54.0 + github.com/vividvilla/simplesessions/stores/redis/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 +) + +require ( + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/klauspost/compress v1.17.7 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect +) + +replace ( + github.com/vividvilla/simplesessions/stores/redis/v3 => ../../stores/redis + github.com/vividvilla/simplesessions/v3 => ../.. +) diff --git a/examples/fasthttp-redis/main.go b/examples/fasthttp-redis/main.go index 9ba97e9..617af78 100644 --- a/examples/fasthttp-redis/main.go +++ b/examples/fasthttp-redis/main.go @@ -1,37 +1,39 @@ package main import ( + "context" "fmt" + "log" "net/http" "time" - "github.com/gomodule/redigo/redis" + "github.com/redis/go-redis/v9" "github.com/valyala/fasthttp" - redisstore "github.com/vividvilla/simplesessions/stores/redis/v2" + redisstore "github.com/vividvilla/simplesessions/stores/redis/v3" "github.com/vividvilla/simplesessions/v3" ) var ( - sessionManager *simplesessions.Manager + sessMgr *simplesessions.Manager testKey = "abc123" testValue = 123456 ) func setHandler(ctx *fasthttp.RequestCtx) { - sess, err := sessionManager.Acquire(ctx, ctx, nil) - if err != nil { - ctx.Error(err.Error(), 500) - return + sess, err := sessMgr.Acquire(nil, ctx, ctx) + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(ctx, ctx) } - err = sess.Set(testKey, testValue) if err != nil { ctx.Error(err.Error(), 500) return } - if err = sess.Commit(); err != nil { + err = sess.Set(testKey, testValue) + if err != nil { ctx.Error(err.Error(), 500) return } @@ -40,7 +42,7 @@ func setHandler(ctx *fasthttp.RequestCtx) { } func getHandler(ctx *fasthttp.RequestCtx) { - sess, err := sessionManager.Acquire(ctx, ctx, nil) + sess, err := sessMgr.Acquire(ctx, ctx, nil) if err != nil { ctx.Error(err.Error(), 500) return @@ -100,34 +102,33 @@ func setCookie(cookie *http.Cookie, w interface{}) error { return nil } -func getRedisPool(address string, password string, maxActive int, maxIdle int, timeout time.Duration) *redis.Pool { - return &redis.Pool{ - Wait: true, - MaxActive: maxActive, - MaxIdle: maxIdle, - Dial: func() (redis.Conn, error) { - c, err := redis.Dial( - "tcp", - address, - redis.DialPassword(password), - redis.DialConnectTimeout(timeout), - redis.DialReadTimeout(timeout), - redis.DialWriteTimeout(timeout), - ) - - return c, err - }, +func getRedisPool() redis.UniversalClient { + o := &redis.Options{ + Addr: "localhost:6379", + Username: "", + Password: "", + DialTimeout: time.Second * 3, + DB: 0, + } + + var ( + ctx = context.TODO() + cl = redis.NewClient(o) + ) + if err := cl.Ping(ctx).Err(); err != nil { + log.Fatalf("error initializing redis: %v", err) } + + return cl } func main() { - rPool := getRedisPool("localhost:6379", "", 10, 10, 1000*time.Millisecond) + rPool := getRedisPool() - sessionManager = simplesessions.New(simplesessions.Options{}) - store := redisstore.New(rPool) - sessionManager.UseStore(store) - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + store := redisstore.New(context.TODO(), rPool) + sessMgr.UseStore(store) + sessMgr.SetCookieHooks(getCookie, setCookie) m := func(ctx *fasthttp.RequestCtx) { switch string(ctx.Path()) { diff --git a/examples/nethttp-inmemory/go.mod b/examples/nethttp-inmemory/go.mod index 875c58b..4f185bb 100644 --- a/examples/nethttp-inmemory/go.mod +++ b/examples/nethttp-inmemory/go.mod @@ -1,8 +1,13 @@ module github.com/vividvilla/simplesessions/examples/nethttp-inmemory -go 1.14 +go 1.18 require ( - github.com/vividvilla/simplesessions/stores/memory/v2 v2.0.0 - github.com/vividvilla/simplesessions/v3 v2.0.0 + github.com/vividvilla/simplesessions/stores/memory/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 +) + +replace ( + github.com/vividvilla/simplesessions/stores/memory/v3 => ../../stores/memory + github.com/vividvilla/simplesessions/v3 => ../.. ) diff --git a/examples/nethttp-inmemory/main.go b/examples/nethttp-inmemory/main.go index 1b67e1f..d79bed9 100644 --- a/examples/nethttp-inmemory/main.go +++ b/examples/nethttp-inmemory/main.go @@ -5,31 +5,31 @@ import ( "log" "net/http" - "github.com/vividvilla/simplesessions/stores/memory/v2" + "github.com/vividvilla/simplesessions/stores/memory/v3" "github.com/vividvilla/simplesessions/v3" ) var ( - sessionManager *simplesessions.Manager - + sessMgr *simplesessions.Manager testKey = "abc123" testValue = 123456 ) func setHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) - if err != nil { - http.Error(w, err.Error(), 500) - return + sess, err := sessMgr.Acquire(nil, r, w) + + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(r, w) } - err = sess.Set(testKey, testValue) if err != nil { http.Error(w, err.Error(), 500) return } - if err = sess.Commit(); err != nil { + err = sess.Set(testKey, testValue) + if err != nil { http.Error(w, err.Error(), 500) return } @@ -38,7 +38,7 @@ func setHandler(w http.ResponseWriter, r *http.Request) { } func getHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) + sess, err := sessMgr.Acquire(nil, r, w) if err != nil { http.Error(w, err.Error(), 500) return @@ -70,10 +70,9 @@ func setCookie(cookie *http.Cookie, w interface{}) error { } func main() { - sessionManager = simplesessions.New(simplesessions.Options{}) - sessionManager.UseStore(memory.New()) - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + sessMgr.UseStore(memory.New()) + sessMgr.SetCookieHooks(getCookie, setCookie) http.HandleFunc("/set", setHandler) http.HandleFunc("/get", getHandler) diff --git a/examples/nethttp-redis/go.mod b/examples/nethttp-redis/go.mod index 31053d9..3d7718e 100644 --- a/examples/nethttp-redis/go.mod +++ b/examples/nethttp-redis/go.mod @@ -1,9 +1,19 @@ module github.com/vividvilla/simplesessions/examples/nethttp-redis -go 1.14 +go 1.18 require ( - github.com/gomodule/redigo v2.0.0+incompatible - github.com/vividvilla/simplesessions/stores/redis/v2 v2.0.0 - github.com/vividvilla/simplesessions/v3 v2.0.0 + github.com/redis/go-redis/v9 v9.5.1 + github.com/vividvilla/simplesessions/stores/redis/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect +) + +replace ( + github.com/vividvilla/simplesessions/stores/redis/v3 => ../../stores/redis + github.com/vividvilla/simplesessions/v3 => ../.. ) diff --git a/examples/nethttp-redis/main.go b/examples/nethttp-redis/main.go index 6e0dd64..e2d525b 100644 --- a/examples/nethttp-redis/main.go +++ b/examples/nethttp-redis/main.go @@ -1,37 +1,39 @@ package main import ( + "context" "fmt" "log" "net/http" "time" - "github.com/gomodule/redigo/redis" - redisstore "github.com/vividvilla/simplesessions/stores/redis/v2" + "github.com/redis/go-redis/v9" + redisstore "github.com/vividvilla/simplesessions/stores/redis/v3" "github.com/vividvilla/simplesessions/v3" ) var ( - sessionManager *simplesessions.Manager + sessMgr *simplesessions.Manager testKey = "abc123" testValue = 123456 ) func setHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) - if err != nil { - http.Error(w, err.Error(), 500) - return + sess, err := sessMgr.Acquire(nil, r, w) + + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(r, w) } - err = sess.Set(testKey, testValue) if err != nil { http.Error(w, err.Error(), 500) return } - if err = sess.Commit(); err != nil { + err = sess.Set(testKey, testValue) + if err != nil { http.Error(w, err.Error(), 500) return } @@ -40,7 +42,7 @@ func setHandler(w http.ResponseWriter, r *http.Request) { } func getHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) + sess, err := sessMgr.Acquire(nil, r, w) if err != nil { http.Error(w, err.Error(), 500) return @@ -71,34 +73,31 @@ func setCookie(cookie *http.Cookie, w interface{}) error { return nil } -func getRedisPool(address string, password string, maxActive int, maxIdle int, timeout time.Duration) *redis.Pool { - return &redis.Pool{ - Wait: true, - MaxActive: maxActive, - MaxIdle: maxIdle, - Dial: func() (redis.Conn, error) { - c, err := redis.Dial( - "tcp", - address, - redis.DialPassword(password), - redis.DialConnectTimeout(timeout), - redis.DialReadTimeout(timeout), - redis.DialWriteTimeout(timeout), - ) - - return c, err - }, +func getRedisPool() redis.UniversalClient { + o := &redis.Options{ + Addr: "localhost:6379", + Username: "", + Password: "", + DialTimeout: time.Second * 3, + DB: 0, + } + + var ( + ctx = context.TODO() + cl = redis.NewClient(o) + ) + if err := cl.Ping(ctx).Err(); err != nil { + log.Fatalf("error initializing redis: %v", err) } + + return cl } func main() { - rPool := getRedisPool("localhost:6379", "", 10, 10, 1000*time.Millisecond) - - sessionManager = simplesessions.New(simplesessions.Options{}) - store := redisstore.New(rPool) - sessionManager.UseStore(store) - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + store := redisstore.New(context.Background(), getRedisPool()) + sessMgr.UseStore(store) + sessMgr.SetCookieHooks(getCookie, setCookie) http.HandleFunc("/set", setHandler) http.HandleFunc("/get", getHandler) diff --git a/examples/nethttp-secure-cookie/go.mod b/examples/nethttp-secure-cookie/go.mod index c55d285..b620acd 100644 --- a/examples/nethttp-secure-cookie/go.mod +++ b/examples/nethttp-secure-cookie/go.mod @@ -1,10 +1,15 @@ module github.com/vividvilla/simplesessions/examples/nethttp-secure-cookie -go 1.14 +go 1.18 require ( - github.com/vividvilla/simplesessions/stores/securecookie/v2 v2.0.0 - github.com/vividvilla/simplesessions/v3 v2.0.0 + github.com/vividvilla/simplesessions/stores/securecookie/v3 v3.0.0 + github.com/vividvilla/simplesessions/v3 v3.0.0 ) require github.com/gorilla/securecookie v1.1.2 // indirect + +replace ( + github.com/vividvilla/simplesessions/stores/securecookie/v3 => ../../stores/securecookie + github.com/vividvilla/simplesessions/v3 => ../.. +) diff --git a/examples/nethttp-secure-cookie/main.go b/examples/nethttp-secure-cookie/main.go index f956d45..3999476 100644 --- a/examples/nethttp-secure-cookie/main.go +++ b/examples/nethttp-secure-cookie/main.go @@ -5,12 +5,12 @@ import ( "log" "net/http" - "github.com/vividvilla/simplesessions/stores/securecookie/v2" + "github.com/vividvilla/simplesessions/stores/securecookie/v3" "github.com/vividvilla/simplesessions/v3" ) var ( - sessionManager *simplesessions.Manager + sessMgr *simplesessions.Manager store = securecookie.New( []byte("0dIHy6S2uBuKaNnTUszB218L898ikGYA"), @@ -22,7 +22,21 @@ var ( ) func setHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) + sess, err := sessMgr.Acquire(nil, r, w) + // Create new session if it doesn't exist. + if err == simplesessions.ErrInvalidSession { + sess, err = sessMgr.NewSession(r, w) + + // IMPORTANT: any Set/SetMulti/Delete/Clear/Destroy and NewSession() + // should flush the values using `store.Flush()` otherwise cookie won't be updated. + if err == nil { + ck, err := store.Flush(sess.ID()) + if err == nil { + err = sess.WriteCookie(ck) + } + } + } + if err != nil { http.Error(w, err.Error(), 500) return @@ -52,7 +66,7 @@ func setHandler(w http.ResponseWriter, r *http.Request) { } func getHandler(w http.ResponseWriter, r *http.Request) { - sess, err := sessionManager.Acquire(r, w, nil) + sess, err := sessMgr.Acquire(nil, r, w) if err != nil { http.Error(w, err.Error(), 500) return @@ -84,11 +98,9 @@ func setCookie(cookie *http.Cookie, w interface{}) error { } func main() { - sessionManager = simplesessions.New(simplesessions.Options{}) - sessionManager.UseStore(store) - - sessionManager.RegisterGetCookie(getCookie) - sessionManager.RegisterSetCookie(setCookie) + sessMgr = simplesessions.New(simplesessions.Options{}) + sessMgr.UseStore(store) + sessMgr.SetCookieHooks(getCookie, setCookie) http.HandleFunc("/set", setHandler) http.HandleFunc("/get", getHandler) diff --git a/go.work b/go.work index e220ee0..91460e8 100644 --- a/go.work +++ b/go.work @@ -1,9 +1,9 @@ -go 1.14 +go 1.18 use ( . ./stores/memory + ./stores/postgres ./stores/redis ./stores/securecookie - ./stores/postgres ) diff --git a/stores/postgres/go.mod b/stores/postgres/go.mod index beb81cc..bb46336 100644 --- a/stores/postgres/go.mod +++ b/stores/postgres/go.mod @@ -1,8 +1,14 @@ module github.com/vividvilla/simplesessions/stores/postgres/v3 -go 1.14 +go 1.18 require ( github.com/lib/pq v1.10.9 github.com/stretchr/testify v1.9.0 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +)