Skip to content

Commit

Permalink
Add keys related methods for context
Browse files Browse the repository at this point in the history
  • Loading branch information
wengchaoxi committed Apr 17, 2022
1 parent c4485bc commit 118e8c1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion framework/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewContext(req *http.Request, rw http.ResponseWriter) *Context {
ctx: req.Context(),
index: -1,
mu: &sync.RWMutex{},
keys: make(map[string]interface{}),
}
}

Expand Down Expand Up @@ -61,6 +60,29 @@ func (c *Context) HasTimeout() bool {
return c.hasTimeout
}

func (c *Context) Set(key string, value interface{}) {
c.mu.Lock()
if c.keys == nil {
c.keys = make(map[string]interface{})
}
c.keys[key] = value
c.mu.Unlock()
}

func (c *Context) Get(key string) (value interface{}, exists bool) {
c.mu.RLock()
value, exists = c.keys[key]
c.mu.Unlock()
return
}

func (c *Context) GetString(key string) (s string) {
if value, ok := c.Get(key); ok && value != nil {
s, _ = value.(string)
}
return
}

// 设置 handlers 调用链
func (c *Context) SetHandlers(handlers []HandlerFunc) {
c.handlers = handlers
Expand Down

0 comments on commit 118e8c1

Please sign in to comment.