Skip to content
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sudo: false
language: go
go:
- 1.7
- 1.8
before_install:
- go get -t -v ./...
- go get github.com/mattn/goveralls
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
if session.UserID == "" {
session.UserID = "x"
session.Name = "y"
Expand All @@ -64,4 +64,8 @@ func main() {

## Other Store Implementations

* https://github.com/mushroomsir/session-redis -Redis
* https://github.com/mushroomsir/session-redis -Redis

## Other Application Implementations

* https://github.com/teambition/gear-session
10 changes: 4 additions & 6 deletions cookiestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import "github.com/go-http-utils/cookie"
//
// Fields are a subset of http.Cookie fields.
type Options struct {
Path string
Domain string
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
// MaxAge>0 means Max-Age attribute present and given in seconds.
Path string
Domain string
MaxAge int
Secure bool
HTTPOnly bool
Expand Down Expand Up @@ -45,8 +42,9 @@ type CookieStore struct {
func (c *CookieStore) Load(name string, session Sessions, cookie *cookie.Cookies) error {
val, err := cookie.Get(name, c.opts.Signed)
if val != "" {
Decode(val, &session)
err = Decode(val, &session)
}
// should call Init even if err
session.Init(name, val, cookie, c, val)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
if session.UserID == "" {
session.UserID = "x"
session.Name = "y"
Expand All @@ -49,7 +49,7 @@ func main() {
store = sessions.New()
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))

println(session.UserID)
println(session.Name)
Expand Down
4 changes: 4 additions & 0 deletions sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (

// Store is an interface for custom session stores.
type Store interface {
// Load should load data from cookie and store, set it into session instance.
// error indicates that session validation failed, or other thing.
// Sessions.Init should be called in Load, even if error occured.
Load(name string, session Sessions, cookie *cookie.Cookies) error
// Save should persist session to the underlying store implementation.
Save(session Sessions) error
}
Expand Down
22 changes: 11 additions & 11 deletions sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSessions(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroom"
session.Age = 99
err = session.Save()
Expand All @@ -54,7 +54,7 @@ func TestSessions(t *testing.T) {
store = sessions.New()
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))

assert.Equal("mushroom", session.Name)
assert.Equal(int64(99), session.Age)
Expand All @@ -71,7 +71,7 @@ func TestSessions(t *testing.T) {
store = sessions.New()
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))

assert.Equal("mushroom", session.Name)
assert.Equal(int64(99), session.Age)
Expand All @@ -88,13 +88,13 @@ func TestSessions(t *testing.T) {
store := sessions.New()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroom"
session.Age = 99
session.Save()

session = &Session{Meta: &sessions.Meta{}}
store.Load(NewSessionName, session, cookie.New(w, r, SessionKeys))
store.Load(NewSessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroomnew"
session.Age = 100
session.Save()
Expand All @@ -109,13 +109,13 @@ func TestSessions(t *testing.T) {
store = sessions.New()
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))

assert.Equal("mushroom", session.Name)
assert.Equal(int64(99), session.Age)

session = &Session{Meta: &sessions.Meta{}}
store.Load(NewSessionName, session, cookie.New(w, r, SessionKeys))
store.Load(NewSessionName, session, cookie.New(w, r, SessionKeys...))

assert.Equal("mushroomnew", session.Name)
assert.Equal(int64(100), session.Age)
Expand All @@ -140,7 +140,7 @@ func TestSessions(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroom"
session.Age = 99

Expand Down Expand Up @@ -177,7 +177,7 @@ func TestSessions(t *testing.T) {
store := sessions.New()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroom"
session.Age = 99

Expand All @@ -192,7 +192,7 @@ func TestSessions(t *testing.T) {
store = sessions.New()
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := &Session{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
session.Name = "mushroom"
session.Age = 99
session.Save()
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestSessionCompatible(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

session := &TbSession{Meta: &sessions.Meta{}}
store.Load(SessionName, session, cookie.New(w, r, SessionKeys))
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))

assert.Equal(int64(1485158874813), session.AuthUpdated)
assert.Equal("http://project.ci/projects", session.NextURL)
Expand Down