session

package module
v0.0.0-...-96d2e9c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2016 License: MIT Imports: 6 Imported by: 2

README

Session Middleware for restgo GoDoc

This package only contains cookie session store, you need implement other store if you want it. Session store must implements Store interface.

Install

    go get github.com/restgo/session

Session Store

  1. Cookie Store (included in this package)
  2. Mongo Store

Exampe example/app.go

    sessionOpts := `{
        "Secret"     :"secret",
        "Secure"     :false,
        "Path"       :"/",
        "HttpOnly"   :true,
        "CookieName" :"cookie-session",
        "MaxAge"     : 84600,
        "EncyptCookie": false
    }`
    
    app.Use(session.NewSessionManager(session.NewCookieSessionStore(), sessionOpts))
    
    app.GET("/about", func(ctx *restgo.Context, next restgo.Next) {
        s := ctx.UserValue("session")
        session, _ := s.(*session.Session)
        if _, ok := session.Values["time"]; ok {
            fmt.Println(session.Values["time"])
        } else {
            session.Values["time"] = time.Now().Format("2006-01-02 15:04:05")
        }
        ctx.ServeText(200, "About")
    })

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSessionManager

func NewSessionManager(store Store, options string) restgo.HTTPHandler

router.Use("/", NewSessionManager(newCookieStore(cookieStoreConfig), sessionManagerConfig)) name: name for session id in cookie, default sid

Types

type CookieStore

type CookieStore struct{}

func NewCookieSessionStore

func NewCookieSessionStore() *CookieStore

func (*CookieStore) Destroy

func (this *CookieStore) Destroy(sid interface{}) error

Destroy session by id

func (*CookieStore) Get

func (this *CookieStore) Get(sid interface{}) (*Session, error)

for cookie store, sid will be session value

func (*CookieStore) Init

func (this *CookieStore) Init(options string) error

do nothing

func (*CookieStore) Save

func (this *CookieStore) Save(session *Session) (interface{}, error)

Save Session, do nothing, return sessions as sid

func (*CookieStore) StoreName

func (this *CookieStore) StoreName() string

store name, "cookie"

type Session

type Session struct {
	Sid string

	Values map[string]interface{} // session data
	// contains filtered or unexported fields
}

func NewSession

func NewSession(store Store, sid string, values map[string]interface{}) *Session

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

type Store

type Store interface {
	Init(options string) error // give options to store, so that store can connect to db, or store options
	Get(sid interface{}) (*Session, error)
	Save(session *Session) (interface{}, error) // save session to store, update expire date, nothing need to do for cookie session
	Destroy(sid interface{}) error
	StoreName() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL