...

Source file src/github.com/gin-gonic/contrib/sessions/sessions.go

Documentation: github.com/gin-gonic/contrib/sessions

     1  package sessions
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/gorilla/context"
     9  	"github.com/gorilla/sessions"
    10  )
    11  
    12  const (
    13  	DefaultKey  = "github.com/gin-gonic/contrib/sessions"
    14  	errorFormat = "[sessions] ERROR! %s\n"
    15  )
    16  
    17  type Store interface {
    18  	sessions.Store
    19  	Options(Options)
    20  }
    21  
    22  // Options stores configuration for a session or session store.
    23  // Fields are a subset of http.Cookie fields.
    24  type Options struct {
    25  	Path   string
    26  	Domain string
    27  	// MaxAge=0 means no 'Max-Age' attribute specified.
    28  	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
    29  	// MaxAge>0 means Max-Age attribute present and given in seconds.
    30  	MaxAge   int
    31  	Secure   bool
    32  	HttpOnly bool
    33  }
    34  
    35  // Wraps thinly gorilla-session methods.
    36  // Session stores the values and optional configuration for a session.
    37  type Session interface {
    38  	// Get returns the session value associated to the given key.
    39  	Get(key interface{}) interface{}
    40  	// Set sets the session value associated to the given key.
    41  	Set(key interface{}, val interface{})
    42  	// Delete removes the session value associated to the given key.
    43  	Delete(key interface{})
    44  	// Clear deletes all values in the session.
    45  	Clear()
    46  	// AddFlash adds a flash message to the session.
    47  	// A single variadic argument is accepted, and it is optional: it defines the flash key.
    48  	// If not defined "_flash" is used by default.
    49  	AddFlash(value interface{}, vars ...string)
    50  	// Flashes returns a slice of flash messages from the session.
    51  	// A single variadic argument is accepted, and it is optional: it defines the flash key.
    52  	// If not defined "_flash" is used by default.
    53  	Flashes(vars ...string) []interface{}
    54  	// Options sets configuration for a session.
    55  	Options(Options)
    56  	// Save saves all sessions used during the current request.
    57  	Save() error
    58  }
    59  
    60  func Sessions(name string, store Store) gin.HandlerFunc {
    61  	return func(c *gin.Context) {
    62  		s := &session{name, c.Request, store, nil, false, c.Writer}
    63  		c.Set(DefaultKey, s)
    64  		defer context.Clear(c.Request)
    65  		c.Next()
    66  	}
    67  }
    68  
    69  type session struct {
    70  	name    string
    71  	request *http.Request
    72  	store   Store
    73  	session *sessions.Session
    74  	written bool
    75  	writer  http.ResponseWriter
    76  }
    77  
    78  func (s *session) Get(key interface{}) interface{} {
    79  	return s.Session().Values[key]
    80  }
    81  
    82  func (s *session) Set(key interface{}, val interface{}) {
    83  	s.Session().Values[key] = val
    84  	s.written = true
    85  }
    86  
    87  func (s *session) Delete(key interface{}) {
    88  	delete(s.Session().Values, key)
    89  	s.written = true
    90  }
    91  
    92  func (s *session) Clear() {
    93  	for key := range s.Session().Values {
    94  		s.Delete(key)
    95  	}
    96  }
    97  
    98  func (s *session) AddFlash(value interface{}, vars ...string) {
    99  	s.Session().AddFlash(value, vars...)
   100  	s.written = true
   101  }
   102  
   103  func (s *session) Flashes(vars ...string) []interface{} {
   104  	s.written = true
   105  	return s.Session().Flashes(vars...)
   106  }
   107  
   108  func (s *session) Options(options Options) {
   109  	s.Session().Options = &sessions.Options{
   110  		Path:     options.Path,
   111  		Domain:   options.Domain,
   112  		MaxAge:   options.MaxAge,
   113  		Secure:   options.Secure,
   114  		HttpOnly: options.HttpOnly,
   115  	}
   116  }
   117  
   118  func (s *session) Save() error {
   119  	if s.Written() {
   120  		e := s.Session().Save(s.request, s.writer)
   121  		if e == nil {
   122  			s.written = false
   123  		}
   124  		return e
   125  	}
   126  	return nil
   127  }
   128  
   129  func (s *session) Session() *sessions.Session {
   130  	if s.session == nil {
   131  		var err error
   132  		s.session, err = s.store.Get(s.request, s.name)
   133  		if err != nil {
   134  			log.Printf(errorFormat, err)
   135  		}
   136  	}
   137  	return s.session
   138  }
   139  
   140  func (s *session) Written() bool {
   141  	return s.written
   142  }
   143  
   144  // shortcut to get session
   145  func Default(c *gin.Context) Session {
   146  	return c.MustGet(DefaultKey).(Session)
   147  }
   148  

View as plain text