README
¶
XORM backend for gorilla sessions
go get github.com/lafriks/xormstore
Example
// initialize and setup cleanup
store := xormstore.New(engine, []byte("secret"))
// db cleanup every hour
// close quit channel to stop cleanup
quit := make(chan struct{})
go store.PeriodicCleanup(1*time.Hour, quit)
// in HTTP handler
func handlerFunc(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session")
session.Values["user_id"] = 123
store.Save(r, w, session)
http.Error(w, "", http.StatusOK)
}
For more details see xormstore godoc documentation.
Testing
Just sqlite3 tests:
go test
All databases using docker:
./test
If docker is not local (docker-machine etc):
DOCKER_IP=$(docker-machine ip dev) ./test
License
xormstore is licensed under the MIT license. See LICENSE for the full license text.
Documentation
¶
Overview ¶
Package xormstore is a XORM backend for gorilla sessions
Simplest form:
store, err := xormstore.New(engine, []byte("secret-hash-key"))
All options:
store, err := xormstore.NewOptions( engine, // *xorm.Engine xormstore.Options{ TableName: "sessions", // "sessions" is default SkipCreateTable: false, // false is default }, []byte("secret-hash-key"), // 32 or 64 bytes recommended, required []byte("secret-encyption-key")) // nil, 16, 24 or 32 bytes, optional if err != nil { // xormstore can not be initialized } // some more settings, see sessions.Options store.SessionOpts.Secure = true store.SessionOpts.HttpOnly = true store.SessionOpts.MaxAge = 60 * 60 * 24 * 60
If you want periodic cleanup of expired sessions:
quit := make(chan struct{}) go store.PeriodicCleanup(1*time.Hour, quit)
For more information about the keys see https://github.com/gorilla/securecookie
For API to use in HTTP handlers see https://github.com/gorilla/sessions
Index ¶
- type Options
- type Store
- func (st *Store) Cleanup()
- func (st *Store) Get(r *http.Request, name string) (*sessions.Session, error)
- func (st *Store) MaxAge(age int)
- func (st *Store) MaxLength(l int)
- func (st *Store) New(r *http.Request, name string) (*sessions.Session, error)
- func (st *Store) PeriodicCleanup(interval time.Duration, quit <-chan struct{})
- func (st *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct { Codecs []securecookie.Codec SessionOpts *sessions.Options // contains filtered or unexported fields }
Store represent a xormstore
func NewOptions ¶
NewOptions creates a new xormstore session with options
func (*Store) MaxAge ¶
MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.
func (*Store) MaxLength ¶
MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default is 4096 (default for securecookie)
func (*Store) PeriodicCleanup ¶
PeriodicCleanup runs Cleanup every interval. Close quit channel to stop.