Skip to content

Commit

Permalink
fix: update examples to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed May 31, 2024
1 parent 9aa8451 commit 1b74c80
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 154 deletions.
13 changes: 0 additions & 13 deletions examples/fastglue-goredis/go.mod

This file was deleted.

30 changes: 30 additions & 0 deletions examples/fastglue-redis/go.mod
Original file line number Diff line number Diff line change
@@ -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 => ../..
)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions examples/fasthttp-inmemory/go.mod
Original file line number Diff line number Diff line change
@@ -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 => ../..
)
27 changes: 13 additions & 14 deletions examples/fasthttp-inmemory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
19 changes: 13 additions & 6 deletions examples/fasthttp-redis/go.mod
Original file line number Diff line number Diff line change
@@ -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 => ../..
)
23 changes: 23 additions & 0 deletions examples/fasthttp-redis/go.mod.bak
Original file line number Diff line number Diff line change
@@ -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 => ../..
)
67 changes: 34 additions & 33 deletions examples/fasthttp-redis/main.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
11 changes: 8 additions & 3 deletions examples/nethttp-inmemory/go.mod
Original file line number Diff line number Diff line change
@@ -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 => ../..
)
Loading

0 comments on commit 1b74c80

Please sign in to comment.