session

package
v0.0.0-...-9d85d00 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	Store
	Propagator
	SessCtxKey string // 在 context 中的备份,方便使用
}

Manager 为了简化使用,提供了一些常用的方法

func (*Manager) GetSession

func (m *Manager) GetSession(ctx *kyuu.Context) (Session, error)

GetSession attempts to retrieve the Session from the context. If successful, it caches the Session instance in the UserValues of the context.

func (*Manager) InitSession

func (m *Manager) InitSession(ctx *kyuu.Context, id string) (Session, error)

InitSession initializes a session with the given id. It generates a new session and injects the id into the context response.

func (*Manager) RefreshSession

func (m *Manager) RefreshSession(ctx *kyuu.Context) (Session, error)

RefreshSession refreshes the session using the provided context. It returns the updated session or an error if the refresh fails.

func (*Manager) RemoveSession

func (m *Manager) RemoveSession(ctx *kyuu.Context) error

RemoveSession removes the session associated with the given context. It returns an error if the session cannot be found or removed.

type Propagator

type Propagator interface {
	// Inject 将 session id 注入到里面
	// Inject 必须是幂等的
	Inject(id string, writer http.ResponseWriter) error
	// Extract 将 session id 从 http.Request 中提取出来
	// 例如从 cookie 中将 session id 提取出来
	Extract(req *http.Request) (string, error)
	// Remove 将 session id 从 http.ResponseWriter 中删除
	// 例如删除对应的 cookie
	Remove(writer http.ResponseWriter) error
}

Propagator 处理请求中的 session id

type Session

type Session interface {
	// Get 获取 session 的值
	Get(ctx context.Context, key string) (string, error)
	// Set 设置 session 的值
	Set(ctx context.Context, key string, val string) error
	// ID 获取 session 的 ID
	ID() string
}

Session 这个通常是一个接口,用来表示一个 session 结构体必须要实现的方法 session 对应的结构体,需要存在 Store 里面

type Store

type Store interface {
	// Generate 生成一个 session
	Generate(ctx context.Context, id string) (Session, error)
	// Refresh 这种设计是一直用同一个 id 的
	// 如果想支持 Refresh 换 ID,那么可以重新生成一个,并移除原有的
	// 又或者 Refresh(ctx context.Context, id string) (Session, error)
	// 其中返回的是一个新的 Session
	Refresh(ctx context.Context, id string) error
	Remove(ctx context.Context, id string) error
	Get(ctx context.Context, id string) (Session, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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