Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
feat: golam.Context add value getter, setter
Browse files Browse the repository at this point in the history
  • Loading branch information
2rebi committed Jul 5, 2022
1 parent 63a4dab commit d333a02
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const (
type Context interface {
Ctx() context.Context

Set(key interface{}, val interface{})

Get(key interface{}) interface{}

Request() *Req

Response() *Resp
Expand Down Expand Up @@ -88,6 +92,7 @@ type Context interface {
var _ Context = (*contextImpl)(nil)

type contextImpl struct {
ctx context.Context
request *Req
response *Resp
path string
Expand All @@ -104,7 +109,18 @@ type contextImpl struct {
}

func (c *contextImpl) Ctx() context.Context {
return c.Request().Context()
if c.ctx == nil {
c.ctx = c.Request().Context()
}
return c.ctx
}

func (c *contextImpl) Set(key interface{}, val interface{}) {
c.ctx = context.WithValue(c.Ctx(), key, val)
}

func (c *contextImpl) Get(key interface{}) interface{} {
return c.Ctx().Value(key)
}

func (c *contextImpl) writeContentType(value string) {
Expand Down

0 comments on commit d333a02

Please sign in to comment.