Skip to content

v2: minor changes but breaking #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ifndef Q
GOTESTFLAGS += -v
endif

export CGO_ENABLED=0

export GOEXPERIMENT=nocoverageredesign

.PHONY: deps
deps:
$Qgo mod download
Expand Down
14 changes: 9 additions & 5 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"time"

"github.com/abraithwaite/jeff"
redis_store "github.com/abraithwaite/jeff/redis"
"github.com/abraithwaite/jeff/v2"
redis_store "github.com/abraithwaite/jeff/v2/redis"
"github.com/gomodule/redigo/redis"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -58,13 +58,17 @@ func (s *server) registerFormHandler(w http.ResponseWriter, r *http.Request) {
}

func (s *server) internalPageHandler(w http.ResponseWriter, r *http.Request) {
sess := jeff.ActiveSession(r.Context())
sess, _ := jeff.ActiveSession(r.Context())
fmt.Fprintf(w, internalPage, sess.Key)
}

func (s *server) publicPageHandler(w http.ResponseWriter, r *http.Request) {
sess := jeff.ActiveSession(r.Context())
fmt.Fprintf(w, publicPage, sess.Key)
sess, ok := jeff.ActiveSession(r.Context())
if ok {
fmt.Fprintf(w, publicPage, sess.Key)
} else {
fmt.Fprintf(w, publicPage, "anonymous")
}
}

func (s *server) loginHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
16 changes: 12 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module github.com/abraithwaite/jeff
module github.com/abraithwaite/jeff/v2

go 1.14
go 1.18

require (
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737
github.com/gomodule/redigo v1.8.5
github.com/gorilla/handlers v1.4.0
github.com/gorilla/mux v1.7.0
github.com/stretchr/testify v1.5.1
github.com/tinylib/msgp v1.1.6
github.com/stretchr/testify v1.6.1
github.com/vmihailenco/msgpack/v5 v5.3.6-0.20220405065333-233c977ae92b
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
40 changes: 10 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,20 @@ github.com/gorilla/handlers v1.4.0 h1:XulKRWSQK5uChr4pEgSE4Tc/OcmnU9GJuSwdog/tZs
github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9 h1:sEvmEcJVKBNUvgCUClbUQeHOAa9U0I2Ce1BooMvVCY4=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vmihailenco/msgpack/v5 v5.3.6-0.20220405065333-233c977ae92b h1:xt4Y5ZlhaRjsbmgokF33LR0zTf2DiX+x9r4MbcB1GhA=
github.com/vmihailenco/msgpack/v5 v5.3.6-0.20220405065333-233c977ae92b/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtDIs4eL21OuM9nyAADmo=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 17 additions & 5 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"time"

"github.com/abraithwaite/jeff/v2"
"github.com/bradfitz/gomemcache/memcache"
"github.com/vmihailenco/msgpack/v5"
)

// Store satisfies the jeff.Storage interface
Expand All @@ -18,15 +20,19 @@ func New(mc *memcache.Client) *Store {
}

// Store satisfies the jeff.Store.Store method
func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) error {
func (s *Store) Store(ctx context.Context, key []byte, value []jeff.Session, exp time.Time) error {
e := int32(exp.UTC().Unix())

var err error
bts, err := msgpack.Marshal(value)
if err != nil {
return err
}

done := make(chan struct{})
go func() {
err = s.mc.Set(&memcache.Item{
Key: string(key),
Value: value,
Value: bts,
// 2038...
Expiration: e,
})
Expand All @@ -41,7 +47,7 @@ func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) err
}

// Fetch satisfies the jeff.Store.Fetch method
func (s *Store) Fetch(ctx context.Context, key []byte) ([]byte, error) {
func (s *Store) Fetch(ctx context.Context, key []byte) ([]jeff.Session, error) {
var i *memcache.Item
var err error

Expand All @@ -62,7 +68,13 @@ func (s *Store) Fetch(ctx context.Context, key []byte) ([]byte, error) {
if err != nil {
return nil, err
}
return i.Value, nil

var sl []jeff.Session
err = msgpack.Unmarshal(i.Value, &sl)
if err != nil {
return nil, err
}
return sl, nil
}

// Delete satisfies the jeff.Store.Delete method
Expand Down
8 changes: 5 additions & 3 deletions memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"sync"
"time"

"github.com/abraithwaite/jeff/v2"
)

type item struct {
value []byte
value []jeff.Session
exp time.Time
}

Expand All @@ -29,7 +31,7 @@ func New() *Memory {
}

// Store satisfies the jeff.Store.Store method
func (m *Memory) Store(_ context.Context, key, value []byte, exp time.Time) error {
func (m *Memory) Store(_ context.Context, key []byte, value []jeff.Session, exp time.Time) error {
m.rw.Lock()
m.sessions[string(key)] = item{
value: value,
Expand All @@ -40,7 +42,7 @@ func (m *Memory) Store(_ context.Context, key, value []byte, exp time.Time) erro
}

// Fetch satisfies the jeff.Store.Fetch method
func (m *Memory) Fetch(_ context.Context, key []byte) ([]byte, error) {
func (m *Memory) Fetch(_ context.Context, key []byte) ([]jeff.Session, error) {
m.rw.RLock()
v, ok := m.sessions[string(key)]
m.rw.RUnlock()
Expand Down
33 changes: 26 additions & 7 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"strconv"
"time"

"github.com/abraithwaite/jeff/v2"
"github.com/gomodule/redigo/redis"
"github.com/vmihailenco/msgpack/v5"
)

// Store satisfies the jeff.Storage interface
Expand All @@ -23,7 +25,7 @@ func New(p *redis.Pool) *Store {
}

// Store satisfies the jeff.Store.Store method
func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) error {
func (s *Store) Store(ctx context.Context, key []byte, value []jeff.Session, exp time.Time) error {
conn, err := s.pool.GetContext(ctx)
defer conn.Close()
if err != nil {
Expand All @@ -33,7 +35,13 @@ func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) err

done := make(chan struct{})
go func() {
_, err = conn.Do("SETEX", key, strconv.Itoa(e), value)
var bts []byte
bts, err = msgpack.Marshal(value)
if err != nil {
return
}

_, err = conn.Do("SETEX", key, strconv.Itoa(e), bts)
close(done)
}()
select {
Expand All @@ -45,7 +53,7 @@ func (s *Store) Store(ctx context.Context, key, value []byte, exp time.Time) err
}

// Fetch satisfies the jeff.Store.Fetch method
func (s *Store) Fetch(ctx context.Context, key []byte) ([]byte, error) {
func (s *Store) Fetch(ctx context.Context, key []byte) ([]jeff.Session, error) {
conn, err := s.pool.GetContext(ctx)
defer conn.Close()
if err != nil {
Expand All @@ -63,11 +71,22 @@ func (s *Store) Fetch(ctx context.Context, key []byte) ([]byte, error) {
case <-ctx.Done():
return nil, ctx.Err()
case <-done:
if err != nil && err != redis.ErrNil {
return nil, err
}
return bs, nil
}

if err != nil && err != redis.ErrNil {
return nil, err
}
if len(bs) == 0 {
return nil, nil
}

var sl []jeff.Session
err = msgpack.Unmarshal(bs, &sl)
if err != nil {
return nil, err
}

return sl, nil
}

// Delete satisfies the jeff.Store.Delete method
Expand Down
Loading