params_go17.go 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // +build go1.7
  2. package httprouter
  3. import (
  4. "net/http"
  5. )
  6. /*
  7. type paramsKey struct{}
  8. // ParamsKey is the request context key under which URL params are stored.
  9. //
  10. // This is only present from go 1.7.
  11. var ParamsKey = paramsKey{}
  12. // */
  13. // Handler is an adapter which allows the usage of an http.Handler as a
  14. // request handle. With go 1.7+, the Params will be available in the
  15. // request context under ParamsKey.
  16. func (r *Router) Handler(method, path string, handler http.Handler) {
  17. r.Handle(method, path,
  18. func(w http.ResponseWriter, req *http.Request) {
  19. //ctx := req.Context()
  20. //ctx = context.WithValue(ctx, ParamsKey, p)
  21. //req = req.WithContext(ctx)
  22. handler.ServeHTTP(w, req)
  23. },
  24. )
  25. }
  26. // ParamsFromContext pulls the URL parameters from a request context,
  27. // or returns nil if none are present.
  28. //
  29. // This is only present from go 1.7.
  30. /*
  31. func ParamsFromContext(ctx context.Context) Params {
  32. p, _ := ctx.Value(ParamsKey).(Params)
  33. return p
  34. }
  35. // */