sessions

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 7 Imported by: 0

README

Handling Sessions

  • Cyclops has the ability to handle sessions seamlessly
  • Cyclops currenty supports Redis as the session store which implements the Store interface, which means that if Redis is not your thing, you can always implement the methods in the Store interface and you can use whichever backend you like
Initializing session
package main

import (
	"github.com/flannel-dev-lab/cyclops/cookie"
	"github.com/flannel-dev-lab/cyclops/sessions"
)

func main() {
	store := sessions.RedisSessionStore{}
	err := store.New("tcp", ":6379")
	if err != nil {
		panic(err)
	}
	cookieobj := cookie.CyclopsCookie{
		Secure:         false,
		HttpOnly:       true,
		StrictSameSite: false,
	}

	session := sessions.Session{
		Store:  &store,
		Cookie: cookieobj}
}
Adding data to session
data := make(map[string]interface{})
data["Name"] = "hello"

session.Set(w, data, 10)

The above snippet says set the map data to session with 10 seconds expiry

Get data from session
session.Get("Name")
Delete data from session
session.Delete("Name")
Delete all from backend session store
session.Reset()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisSessionStore added in v0.1.0

type RedisSessionStore struct {
	Pool *redis.Pool
}

func (*RedisSessionStore) Delete added in v0.1.0

func (redisSessionStore *RedisSessionStore) Delete(key string) error

func (*RedisSessionStore) Get added in v0.1.0

func (redisSessionStore *RedisSessionStore) Get(key string) (data map[string]interface{}, err error)

func (*RedisSessionStore) New added in v0.1.0

func (redisSessionStore *RedisSessionStore) New(network, address string) error

func (*RedisSessionStore) Reset added in v0.1.0

func (redisSessionStore *RedisSessionStore) Reset() error

func (*RedisSessionStore) Save added in v0.1.0

func (redisSessionStore *RedisSessionStore) Save(key string, value map[string]interface{}, expiry time.Duration) (err error)

type Session added in v0.1.0

type Session struct {
	Store  Store
	Cookie cookie.CyclopsCookie
	// contains filtered or unexported fields
}

func (*Session) Delete added in v0.1.0

func (session *Session) Delete(w http.ResponseWriter, r *http.Request) (err error)

Delete only deletes a particular session from database

func (*Session) Get added in v0.1.0

func (session *Session) Get(r *http.Request) (data map[string]interface{}, err error)

Get Retrieves a particular session with key "session_id"

func (*Session) Reset added in v0.1.0

func (session *Session) Reset(r *http.Request) (err error)

Reset deletes all session values from the database

func (*Session) Set added in v0.1.0

func (session *Session) Set(w http.ResponseWriter, data map[string]interface{}, expiry time.Duration) (err error)

Set will set a session to the database with a map of data and expiry. Once written, it will send the session_id as cookie

func (*Session) Update added in v1.1.10

func (session *Session) Update(w http.ResponseWriter, r *http.Request, data map[string]interface{}) (err error)

Update updates the existing session with the data passes

type Store added in v0.1.0

type Store interface {
	// Save helps in saving data to session store with a expiry
	Save(key string, data map[string]interface{}, expiry time.Duration) error
	// Get retrieves the data associated with the session key
	Get(key string) (data map[string]interface{}, err error)
	// Delete deletes a session from session store
	Delete(key string) error
	// Reset deletes all sessions from session store
	Reset() error
}

Store provides interface to create custom session stores

Jump to

Keyboard shortcuts

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