github.com/corylanou/buffalo@v0.8.0/session.go (about)

     1  package buffalo
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/sessions"
     7  )
     8  
     9  // Session wraps the "github.com/gorilla/sessions" API
    10  // in something a little cleaner and a bit more useable.
    11  type Session struct {
    12  	Session *sessions.Session
    13  	req     *http.Request
    14  	res     http.ResponseWriter
    15  }
    16  
    17  // Save the current session.
    18  func (s *Session) Save() error {
    19  	return s.Session.Save(s.req, s.res)
    20  }
    21  
    22  // Get a value from the current session.
    23  func (s *Session) Get(name interface{}) interface{} {
    24  	return s.Session.Values[name]
    25  }
    26  
    27  // Set a value onto the current session. If a value with that name
    28  // already exists it will be overridden with the new value.
    29  func (s *Session) Set(name, value interface{}) {
    30  	s.Session.Values[name] = value
    31  }
    32  
    33  // Delete a value from the current session.
    34  func (s *Session) Delete(name interface{}) {
    35  	delete(s.Session.Values, name)
    36  }
    37  
    38  // Clear the current session
    39  func (s *Session) Clear() {
    40  	for k := range s.Session.Values {
    41  		s.Delete(k)
    42  	}
    43  }
    44  
    45  // Get a session using a request and response.
    46  func (a *App) getSession(r *http.Request, w http.ResponseWriter) *Session {
    47  	session, _ := a.SessionStore.Get(r, a.SessionName)
    48  	return &Session{
    49  		Session: session,
    50  		req:     r,
    51  		res:     w,
    52  	}
    53  }