simplesession

package module
v0.0.0-...-b2b909e Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2015 License: BSD-2-Clause Imports: 13 Imported by: 0

README

simplesession

File based session support for Golang - Build Status

Features
  • Simple, precise & clean code
  • Fast & efficient, easy to use
  • No dependency injection
  • Using go standard library only (No third-party library included)
  • Best suit for the applications need file based session handling
  • Small to Mid-Level applications
Usage
  1. Create New Session

    New creates a fresh session environment

    option := &simplesession.Option{
        Path:     "/",
        Domain:   "example.com", //your domain
        Expires:  time.Unix(1, 1)  // Optional time for cookie persistent on browser
        MaxAge:   24 * 60 * 60, //expiry time of session cookie
        Secure:   false,
        HttpOnly: true,
    }
    
    session, err := simplesession.New(res, option)
    if err != nil {
        return err
    }
    
    // Do something with session
    
  2. Set Session Data

    simplesession uses map[string]interface{} as its storage for handling data at runtime which greatly improves the efficiency

    session.Set(key, val)
    
    Ex.
    session.Set("id", 101)
    session.Set("uname", "astromahi")
    
  3. Write Session

    Write writes the session data to file for persistent and later use.

    err := session.Write()
    if err != nil {
        return err
    }
    return nil
    
  4. Read Session

    Read reads the data from stored session.
    It takes http.Request and gives stored session

    session, err := simplesession.Read(req)
    if err != nil {
        return err
    }
    
    // Do something with session
    
  5. Get Session Data

    Get returns the stored data from read session

    data := session.Get(key)
    
    Ex.
    userId := session.Get("id") // 101
    uname := session.Get("uname")   // astromahi
    
  6. Delete Session Data

    Del deletes the user session data from session

    session.Del("id")
    

    After deletion, you should save the modified session

    if err := session.Write(res, req); err != nil {
        return err
    }
    return nil
    
  7. Destroy Session Completely

    Destroy destroys the whole session & removes the session file from disk. It takes http.ResponseWriter as parameter

    err := session.Destroy(res)
    if err != nil {
        return err
    }
    return nil
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	Path   string
	Domain string
	// MaxAge=0 means no 'Max-Age' attribute specified.
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
	// MaxAge>0 means Max-Age attribute present and given in seconds.
	MaxAge   int
	Secure   bool
	HttpOnly bool
}

Option stores configuration for a session cookie.

Fields are a subset of http.Cookie fields.

type SimpleSession

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

SimpleSession stores the session data and option configuration for a session.

func New

func New(res http.ResponseWriter, option *Option) (*SimpleSession, error)

New is called to create a new session instance

func Read

func Read(req *http.Request) (*SimpleSession, error)

Read reads stored session from session file

func (*SimpleSession) Del

func (ss *SimpleSession) Del(key string)

Del deletes the session data by key-value pair

func (*SimpleSession) Destroy

func (ss *SimpleSession) Destroy(res http.ResponseWriter) error

Destroy completely destroys the session including data stored on session file

func (*SimpleSession) FilePath

func (ss *SimpleSession) FilePath() string

FilePath gives the session directory where we store the session file

func (*SimpleSession) Get

func (ss *SimpleSession) Get(key string) interface{}

Get retrieves the value by given key from local session variable

func (*SimpleSession) Id

func (ss *SimpleSession) Id() string

Id returns the session id currently in use

func (*SimpleSession) Name

func (ss *SimpleSession) Name() string

Name returns the name of the registered session

func (*SimpleSession) Set

func (ss *SimpleSession) Set(key string, val interface{})

Set stores the value by given key to local session variable

func (*SimpleSession) Write

func (ss *SimpleSession) Write() error

Write flush the locally stored data onto session file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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