1 Star 0 Fork 0

fkil555/gin-extend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
context.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
fkil555 提交于 2023-09-10 21:31 . gin-extend init
package context
import (
"golang.org/x/net/context"
)
// Cron的执行上下文, 类比于gin.Context
type Context struct {
context.Context
Name string // 任务名
Spec string // 任务cron表达式
Chain []HandleFunc // 中间件调用链
ChainIndex int // 执行到chain的哪一环
KeyValues map[string]interface{} // 存储KV
Params []string // 获取参数
}
type HandleFunc func(*Context) // Cron处理函数
// 执行下一个中间件
func (ctx *Context) Next() {
curIndex := ctx.ChainIndex
if curIndex >= len(ctx.Chain) {
return
}
ctx.ChainIndex++
ctx.Chain[curIndex](ctx)
}
func (ctx *Context) Set(key string, value interface{}) {
ctx.KeyValues[key] = value
}
func (ctx *Context) Get(key string) (value interface{}, exist bool) {
value, exist = ctx.KeyValues[key]
return
}
// 复写context.Context的Value方法, 提供键值能力
func (ctx *Context) Value(key interface{}) (value interface{}) {
if strKey, ok := key.(string); ok {
value, _ = ctx.KeyValues[strKey]
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fkil555/gin-extend.git
git@gitee.com:fkil555/gin-extend.git
fkil555
gin-extend
gin-extend
v0.1.22

搜索帮助