stretchr / goweb

A lightweight RESTful web framework for Go
632 stars 61 forks source link

Would be nice to be able to add custom data to the Context to share during the request lifetime #29

Closed oleksandr closed 11 years ago

oleksandr commented 11 years ago

This could be a good example: http://www.gorillatoolkit.org/pkg/context

Thus in the request handling chain (Before -> Controller's Method -> After) it would be really easy to share the data.

As an example we use:

  1. Before: parse Authorization header, extract Token if available, fetch the corresponding user/session data, assign to context such as context.Set(CURRENT_USER_KEY, userStructure)
  2. Controller's method just performs the check context.Get(CURRENT_USER_KEY) != nil

This would help to avoid duplication of code especially related to authentication...

oleksandr commented 11 years ago

It seems that is already possible with c.Data().Set/Get, right?

matryer commented 11 years ago

Right, the context.Data thing is exactly for that. Goweb ignores the Data object.

It's an objects.Map from the http://godoc.org/github.com/stretchr/stew/objects package.