Session

Session is a session middleware for Tango.

Backend Supports

Currently session support some backends below:

  • Memory - memory as a session store
  • Redis - redis server as a session store
  • Ledis - ledis server as a session store
  • nodb - nodb as a session store
  • ssdb - use ssdb server as a session store

Installation

go get github.com/tango-contrib/session

Simple Example

package main

import (
    "github.com/lunny/tango"
    "github.com/tango-contrib/session"
)

type SessionAction struct {
    session.Session
}

func (a *SessionAction) Get() string {
    a.Session.Set("test", "1")
    return a.Session.Get("test").(string)
}

func main() {
    o := tango.Classic()
    o.Use(session.New(session.Options{
        MaxAge:time.Minute * 20,
        }))
    o.Get("/", new(SessionAction))
}

For other store,

func main() {
    o := tango.Classic()
    o.Use(session.New(session.Options{
        MaxAge:time.Minute * 20,
        Store: redistore.New(Options{
			Host:    "127.0.0.1",
			DbIndex: 0,
			MaxAge:  30 * time.Minute,
		},
        }))
    o.Get("/", new(SessionAction))
}