session

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

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

Go to latest
Published: Jul 16, 2021 License: Apache-2.0 Imports: 11 Imported by: 2

README

session

GitHub Language Go Report Card Build Status CircleCI Codecov

How to use it

package main

import (
	"log"
	"net/http"

	"github.com/eucalytus/session"
)

func main() {
	manager := session.NewManager(session.Options{
		MaxInactiveInterval: 1800, MaxAge: 84000, HttpOnly: true,
	},
		session.CreateMemSession,
		//listen session event
		func(s session.Session, event int) {
			if event == session.Created {
				log.Printf("new session is created, sessionId=%s\n", s.GetMaskedSessionId())
			} else if event == session.Destroyed {
				log.Printf("session is destroyed, sessionId=%s\n", s.GetMaskedSessionId())
			} else {
				log.Printf("session is updated, sessionId=%s\n", s.GetMaskedSessionId())
			}
		},
	)

	//private resource
	http.HandleFunc("/private", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s != nil {
			if _, found := s.Get("key"); found {
				response.Write([]byte("OK"))
				return
			}
		}
		response.WriteHeader(http.StatusUnauthorized)
		response.Write([]byte("StatusUnauthorized"))
	})

	//login
	http.HandleFunc("/login", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s == nil {
			temp, err := manager.CreateSession(request, response)
			if err != nil {
				log.Printf("create session failed: %v\n", err)
			}
			s = temp
		}
		s.Set("key", "login")
		response.Write([]byte("OK"))
	})

	//logout
	http.HandleFunc("/logout", func(response http.ResponseWriter, request *http.Request) {
		s := manager.GetSession(request)
		if s != nil {
			s.Set("key", nil)
			s.Invalidate()
		}
		response.Write([]byte("OK"))
	})

	http.ListenAndServe("0.0.0.0:8000", nil)
}

Documentation

Index

Constants

View Source
const (
	Created   = 1
	Destroyed = 2
	Update    = 3
)
View Source
const DefaultKey = "session-key"

Variables

This section is empty.

Functions

func GetSessionSize

func GetSessionSize(c *gin.Context) int

func UseSession

func UseSession(options Options, store gonicSession.Store, sessionHandler func(Session, int)) func(*gin.Context)

UseSession gin session middleware

Types

type Manager

type Manager struct {
	SessionCreator func(r *http.Request, w http.ResponseWriter) (Session, error)
	// contains filtered or unexported fields
}

Manager manage the created sessions

func NewManager

func NewManager(options Options, SessionCreator func(r *http.Request, w http.ResponseWriter) (Session, error), sessionHandler func(Session, int)) *Manager

NewManager init new session manager and start gc

func (*Manager) CreateSession

func (manager *Manager) CreateSession(r *http.Request, w http.ResponseWriter) (Session, error)

func (*Manager) GetSession

func (manager *Manager) GetSession(request *http.Request) Session

GetSession get memory session store http request

func (*Manager) GetSessionById

func (manager *Manager) GetSessionById(sessionId string) Session

GetSessionById get memory session store by sid

func (*Manager) GetSessionSize

func (manager *Manager) GetSessionSize() int

GetSessionSize get all managed session size

type MemSession

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

func (*MemSession) Get

func (session *MemSession) Get(key interface{}) (value interface{}, ok bool)

func (*MemSession) GetId

func (session *MemSession) GetId() string

func (*MemSession) GetLastAccessTime

func (session *MemSession) GetLastAccessTime() int64

func (*MemSession) GetMaskedSessionId

func (session *MemSession) GetMaskedSessionId() string

GetMaskedSessionId session id is critical information, we should mask it

func (*MemSession) Invalidate

func (session *MemSession) Invalidate()

Invalidate invalidate session will remove session from registry

func (*MemSession) Set

func (session *MemSession) Set(key interface{}, value interface{}) error

type Options

type Options struct {
	Path     string
	Domain   string
	MaxAge   int
	Secure   bool
	HttpOnly bool

	MaxInactiveInterval int64
	GcInterval          int
	GcStopChan          <-chan struct{}
	SessionLoader       func() []Session
}

type Session

type Session interface {
	GetId() string
	GetMaskedSessionId() string
	GetLastAccessTime() int64
	Get(key interface{}) (value interface{}, ok bool)
	Set(key interface{}, value interface{}) error
	Invalidate()
}

func CreateMemSession

func CreateMemSession(r *http.Request, w http.ResponseWriter) (Session, error)

func CreateSession

func CreateSession(c *gin.Context) (Session, error)

func GetSession

func GetSession(c *gin.Context) Session

GetSession get the session from gin context

func NewSession

func NewSession(id string, timeAccessed int64) Session

Directories

Path Synopsis
examples
mem

Jump to

Keyboard shortcuts

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