Initial commit
This commit is contained in:
52
session/gorilla/service.go
Normal file
52
session/gorilla/service.go
Normal file
@ -0,0 +1,52 @@
|
||||
package gorilla
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/securecookie"
|
||||
|
||||
"forge.cadoles.com/wpetit/goweb/service/session"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// SessionService is an implementation of service.Session
|
||||
// based on the github.com/gorilla/sessions
|
||||
type SessionService struct {
|
||||
sessionName string
|
||||
store sessions.Store
|
||||
defaultOptions *sessions.Options
|
||||
}
|
||||
|
||||
// Get returns a Session associated with the given HTTP request
|
||||
func (s *SessionService) Get(w http.ResponseWriter, r *http.Request) (session.Session, error) {
|
||||
sess, err := s.store.Get(r, s.sessionName)
|
||||
if err != nil {
|
||||
multiErr, ok := err.(securecookie.MultiError)
|
||||
if !ok || multiErr.Error() != securecookie.ErrMacInvalid.Error() {
|
||||
return nil, errors.Wrap(err, "error while retrieving the session from the request")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
defaultOptions := s.defaultOptionsCopy()
|
||||
sess.Options = &defaultOptions
|
||||
if err := sess.Save(r, w); err != nil {
|
||||
return nil, errors.Wrap(err, "error while saving session")
|
||||
}
|
||||
}
|
||||
return NewSession(sess), nil
|
||||
}
|
||||
|
||||
func (s *SessionService) defaultOptionsCopy() sessions.Options {
|
||||
return *s.defaultOptions
|
||||
}
|
||||
|
||||
// NewSessionService returns a new SessionService backed
|
||||
// by the given Store
|
||||
func NewSessionService(sessionName string, store sessions.Store, defaultOptions *sessions.Options) *SessionService {
|
||||
return &SessionService{
|
||||
sessionName: sessionName,
|
||||
store: store,
|
||||
defaultOptions: defaultOptions,
|
||||
}
|
||||
}
|
70
session/gorilla/session.go
Normal file
70
session/gorilla/session.go
Normal file
@ -0,0 +1,70 @@
|
||||
package gorilla
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/goweb/service/session"
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
// Session is an implementation of the session.Session service
|
||||
// backed by github.com/gorilla/sessions Session
|
||||
type Session struct {
|
||||
sess *sessions.Session
|
||||
}
|
||||
|
||||
// Get retrieve a value from the session
|
||||
func (s *Session) Get(key string) interface{} {
|
||||
return s.sess.Values[key]
|
||||
}
|
||||
|
||||
// Set updates a value in the session
|
||||
func (s *Session) Set(key string, value interface{}) {
|
||||
s.sess.Values[key] = value
|
||||
}
|
||||
|
||||
// Unset remove a value in the session
|
||||
func (s *Session) Unset(key string) {
|
||||
delete(s.sess.Values, key)
|
||||
}
|
||||
|
||||
// AddFlash adds the given flash message to the stack
|
||||
func (s *Session) AddFlash(flashType session.FlashType, message string) {
|
||||
s.sess.AddFlash(message, s.flashKey(flashType))
|
||||
}
|
||||
|
||||
// Flashes retrieves the flash messages of the given types in the stack
|
||||
func (s *Session) Flashes(flashTypes ...session.FlashType) []session.Flash {
|
||||
flashes := make([]session.Flash, 0)
|
||||
for _, ft := range flashTypes {
|
||||
rawFlashes := s.sess.Flashes(s.flashKey(ft))
|
||||
for _, f := range rawFlashes {
|
||||
message := fmt.Sprintf("%v", f)
|
||||
flashes = append(flashes, session.NewBaseFlash(ft, message))
|
||||
}
|
||||
}
|
||||
return flashes
|
||||
}
|
||||
|
||||
func (s *Session) flashKey(flashType session.FlashType) string {
|
||||
return fmt.Sprintf("_%s_flash", flashType)
|
||||
}
|
||||
|
||||
// Save saves the session with its current values
|
||||
func (s *Session) Save(w http.ResponseWriter, r *http.Request) error {
|
||||
return s.sess.Save(r, w)
|
||||
}
|
||||
|
||||
// Delete deletes the session
|
||||
func (s *Session) Delete(w http.ResponseWriter, r *http.Request) error {
|
||||
s.sess.Options = &sessions.Options{
|
||||
MaxAge: -1,
|
||||
}
|
||||
return s.sess.Save(r, w)
|
||||
}
|
||||
|
||||
// NewSession returns a new Session
|
||||
func NewSession(sess *sessions.Session) *Session {
|
||||
return &Session{sess}
|
||||
}
|
Reference in New Issue
Block a user