coocood / jas

A simple and powerful REST API framework for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use ctx.Extra?

Icedroid opened this issue · comments

extra can store extra data generated/used by hook functions, e.g. 'BeforeServe'. ?
can I use like follow:

type request struct {
    reqStartTime time.Time
    requestId string
    reqData map[string]interface {}
}
func BeforeServeV1(ctx *jas.Context) {
    var req request
    req.reqStartTime = time.Now()
    req.requestId = GenUUID()
    d, err := ctx.FindString("d")
    req.reqData,err  = json.Unmarshal([]byte(d))

   ctx.Extra = req
}

reqData will be used in other router method. is it concurrent written safe?

Every request has its own ctx, so it will not be concurrently accessed by multiple goroutine.

ctx.Extra will point to one request address? I used ctx.Extra.reqData no problem?

no problem.

If I used like these, reqData will only be init once? Every request will write reqData, It is not safe.

var (
  reqStartTime time.Time
  requestId string
  reqData map[string]interface {}
)
func BeforeServeV1(ctx *jas.Context) {
    reqStartTime = time.Now()
    requestId = GenUUID()
    d, err := ctx.FindString("d")
    reqData,err  = json.Unmarshal([]byte(d))

}

Don't use it like that.

If I want transfer a variable to the Create router method, I only can use ctx.Extra? Thank for your Good attention!

type Create struct{}

func (*Create) Post(ctx *jas.Context) {
    ctx.Data = false
    req := ctx.Extra.(request) //need assert....
}

Yes.

On Thu, Mar 27, 2014 at 3:04 PM, Icecream notifications@github.com wrote:

If I want transfer a variable to the Create router method, I only can use
ctx.Extra? Thank for your Good intention!

type Create struct{}

func (*Create) Post(ctx *jas.Context) {
ctx.Data = false
req := ctx.Extra.(request) //need assert....
}

Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-38774374
.

Thank you and your attention! Now I work an mobile api on jas. Maybe need more your help.Than you very much!

I think you mean 'attention', you are welcome.