sessions

package module
v0.4.1-0...-228fbad Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2017 License: MIT Imports: 4 Imported by: 0

README

Sessions middleware for Echo

GitHub release Build Status

This is a package of sessions middleware for Echo.
A thin gorilla/sessions wrapper.

Install

$ go get -u github.com/utahta/echo-sessions

Middleware usage

import (
    "github.com/boj/redistore"
    "github.com/utahta/echo-sessions"
    "github.com/labstack/echo"
)

store, _ := redistore.NewRediStore(10, "tcp", ":6379", "", []byte("secret-key"))

e := echo.New()
e.Use(sessions.Sessions("SESSID", store))

Helpers usage

import "github.com/utahta/echo-sessions"

func example(c echo.Context) {
    sessions.Set(c, "key", "value")
    
    dst := sessions.GetRaw(c, "key")
    
    var dst string
    ok, err := sessions.Get(c, "key", &dst)
    
    var dst string
    ok := sessions.MustGet(c, "key", &dst)
    
    sessions.Delete(c, "key")
    
    ok := sessions.Exists(c, "key")
    
    sessions.Clear(c)
    
    sessions.Flashes(c vars...)
    
    sessions.AddFlash(c, value, vars...)
    
    err := sessions.Save(c)
}

Learn more

Get the session handler
s := sessions.MustStart()
Set key and value
s.Set("key", "value")
Get value by key
var v string
ok, err := s.Get("key", &v)

or

var v string
ok := s.MustGet("key", &v)
Get raw value by key
v := s.GetRaw("key") // returns (interface{})
Check key exists
if !s.Exists("key") {
    s.Set("key", "new value")
}

or

var v string
if ok, err := s.Get("example1", &v); !ok && err == nil {
    s.Set("example1", "new value")
}

if ok := s.MustGet("example2", &v); !ok {
    s.Set("example2", "new value")
}
Delete key
s.Delete("key")
Save this session
err := s.Save()

Contributing

  1. Fork it ( https://github.com/utahta/echo-sessions/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound = errors.New("Session not found")
)

Functions

func AddFlash

func AddFlash(c echo.Context, value interface{}, vars ...string)

func Clear

func Clear(c echo.Context)

func Delete

func Delete(c echo.Context, key interface{})

func Exists

func Exists(c echo.Context, key interface{}) bool

func Flashes

func Flashes(c echo.Context, vars ...string) []interface{}

func Get

func Get(c echo.Context, key interface{}, dst interface{}) (bool, error)

func GetRaw

func GetRaw(c echo.Context, key interface{}) interface{}

func MustGet

func MustGet(c echo.Context, key interface{}, dst interface{}) bool

func MustStart

func MustStart(c echo.Context) *session

Get the session handler if get error, cause panic

func Save

func Save(c echo.Context) error

func Sessions

func Sessions(name string, store sessions.Store) echo.MiddlewareFunc

sessions middleware for Echo

func Set

func Set(c echo.Context, key interface{}, v interface{})

func Start

func Start(c echo.Context) (*session, error)

Get the session handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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