session

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 5 Imported by: 0

README

Session Middleware for KangGo

本仓库包含 KangGo 框架的会话(Session)中间件实现。会话中间件允许开发者在请求之间存储和访问用户数据。

功能特性

  • 使用内存存储会话数据(MemoryStore)。
  • 支持自定义会话存储。
  • 自动生成随机 session_id
  • 提供设置和获取会话数据的功能。

使用方法

  1. 创建会话存储实例,例如 MemoryStore
store := NewMemoryStore()
  1. 创建中间件并注册到路由器:
app.Use(session.Middleware(store))
  1. 在处理程序中获取和设置会话数据:
func handler(ctx *session.Context) {
ctx.SetSessionValue("key", "value")
value := ctx.GetSessionValue("key")
}

测试

你可以使用以下命令运行测试:

go test -v ./middleware/session

许可证

此项目基于 MIT 许可证,详情请参阅 LICENSE 文件。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(store Store) core.MiddlewareFunc

New Middleware 中间件函数,用于管理会话

Types

type Context

type Context struct {
	http.ResponseWriter
	Request     *http.Request
	SessionID   string
	SessionData map[string]interface{}
	Store       Store
}

Context 用于处理会话数据的自定义上下文

func (*Context) GetSessionValue

func (ctx *Context) GetSessionValue(key string) interface{}

GetSessionValue 获取会话中的值

func (*Context) SetSessionValue

func (ctx *Context) SetSessionValue(key string, value interface{})

SetSessionValue 设置会话中的值

type MemoryStore

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

MemoryStore 使用内存存储会话数据

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建一个新的 MemoryStore 实例

func (*MemoryStore) Delete

func (store *MemoryStore) Delete(sessionID string)

Delete 删除会话数据

func (*MemoryStore) Get

func (store *MemoryStore) Get(sessionID string) (map[string]interface{}, bool)

Get 获取会话数据

func (*MemoryStore) Set

func (store *MemoryStore) Set(sessionID string, data map[string]interface{})

Set 设置会话数据

type Store

type Store interface {
	Get(sessionID string) (map[string]interface{}, bool)
	Set(sessionID string, data map[string]interface{})
	Delete(sessionID string)
}

Store 定义会话存储的接口

Jump to

Keyboard shortcuts

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