本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8KB

  1. // Copyright 2012 The Gorilla Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package context stores values shared during a request lifetime.
  6. Note: gorilla/context, having been born well before `context.Context` existed,
  7. does not play well > with the shallow copying of the request that
  8. [`http.Request.WithContext`](https://golang.org/pkg/net/http/#Request.WithContext)
  9. (added to net/http Go 1.7 onwards) performs. You should either use *just*
  10. gorilla/context, or moving forward, the new `http.Request.Context()`.
  11. For example, a router can set variables extracted from the URL and later
  12. application handlers can access those values, or it can be used to store
  13. sessions values to be saved at the end of a request. There are several
  14. others common uses.
  15. The idea was posted by Brad Fitzpatrick to the go-nuts mailing list:
  16. http://groups.google.com/group/golang-nuts/msg/e2d679d303aa5d53
  17. Here's the basic usage: first define the keys that you will need. The key
  18. type is interface{} so a key can be of any type that supports equality.
  19. Here we define a key using a custom int type to avoid name collisions:
  20. package foo
  21. import (
  22. "github.com/gorilla/context"
  23. )
  24. type key int
  25. const MyKey key = 0
  26. Then set a variable. Variables are bound to an http.Request object, so you
  27. need a request instance to set a value:
  28. context.Set(r, MyKey, "bar")
  29. The application can later access the variable using the same key you provided:
  30. func MyHandler(w http.ResponseWriter, r *http.Request) {
  31. // val is "bar".
  32. val := context.Get(r, foo.MyKey)
  33. // returns ("bar", true)
  34. val, ok := context.GetOk(r, foo.MyKey)
  35. // ...
  36. }
  37. And that's all about the basic usage. We discuss some other ideas below.
  38. Any type can be stored in the context. To enforce a given type, make the key
  39. private and wrap Get() and Set() to accept and return values of a specific
  40. type:
  41. type key int
  42. const mykey key = 0
  43. // GetMyKey returns a value for this package from the request values.
  44. func GetMyKey(r *http.Request) SomeType {
  45. if rv := context.Get(r, mykey); rv != nil {
  46. return rv.(SomeType)
  47. }
  48. return nil
  49. }
  50. // SetMyKey sets a value for this package in the request values.
  51. func SetMyKey(r *http.Request, val SomeType) {
  52. context.Set(r, mykey, val)
  53. }
  54. Variables must be cleared at the end of a request, to remove all values
  55. that were stored. This can be done in an http.Handler, after a request was
  56. served. Just call Clear() passing the request:
  57. context.Clear(r)
  58. ...or use ClearHandler(), which conveniently wraps an http.Handler to clear
  59. variables at the end of a request lifetime.
  60. The Routers from the packages gorilla/mux and gorilla/pat call Clear()
  61. so if you are using either of them you don't need to clear the context manually.
  62. */
  63. package context
上海开阖软件有限公司 沪ICP备12045867号-1