update context logic in httprp too
Bas van Beek
7 years ago
13 | 13 |
|
14 | 14 |
// Server is a proxying request handler.
|
15 | 15 |
type Server struct {
|
16 | |
ctx context.Context
|
17 | 16 |
proxy http.Handler
|
18 | 17 |
before []RequestFunc
|
19 | 18 |
errorEncoder func(w http.ResponseWriter, err error)
|
|
24 | 23 |
// If the target's path is "/base" and the incoming request was for "/dir",
|
25 | 24 |
// the target request will be for /base/dir.
|
26 | 25 |
func NewServer(
|
27 | |
ctx context.Context,
|
28 | 26 |
baseURL *url.URL,
|
29 | 27 |
options ...ServerOption,
|
30 | 28 |
) *Server {
|
31 | 29 |
s := &Server{
|
32 | |
ctx: ctx,
|
33 | 30 |
proxy: httputil.NewSingleHostReverseProxy(baseURL),
|
34 | 31 |
}
|
35 | 32 |
for _, option := range options {
|
|
49 | 46 |
|
50 | 47 |
// ServeHTTP implements http.Handler.
|
51 | 48 |
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
52 | |
ctx := s.ctx
|
|
49 |
ctx := r.Context()
|
53 | 50 |
|
54 | 51 |
for _, f := range s.before {
|
55 | 52 |
ctx = f(ctx, r)
|
19 | 19 |
originURL, _ := url.Parse(originServer.URL)
|
20 | 20 |
|
21 | 21 |
handler := httptransport.NewServer(
|
22 | |
context.Background(),
|
23 | 22 |
originURL,
|
24 | 23 |
)
|
25 | 24 |
proxyServer := httptest.NewServer(handler)
|
|
54 | 53 |
originURL, _ := url.Parse(originServer.URL)
|
55 | 54 |
|
56 | 55 |
handler := httptransport.NewServer(
|
57 | |
context.Background(),
|
58 | 56 |
originURL,
|
59 | 57 |
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
|
60 | 58 |
r.Header.Add(headerKey, headerVal)
|
|
81 | 79 |
originURL, _ := url.Parse(originServer.URL)
|
82 | 80 |
|
83 | 81 |
handler := httptransport.NewServer(
|
84 | |
context.Background(),
|
85 | 82 |
originURL,
|
86 | 83 |
)
|
87 | 84 |
proxyServer := httptest.NewServer(handler)
|
|
102 | 99 |
originServer.Close()
|
103 | 100 |
|
104 | 101 |
handler := httptransport.NewServer(
|
105 | |
context.Background(),
|
106 | 102 |
originURL,
|
107 | 103 |
)
|
108 | 104 |
proxyServer := httptest.NewServer(handler)
|
|
137 | 133 |
originURL, _ := url.Parse(originServer.URL)
|
138 | 134 |
|
139 | 135 |
handler := httptransport.NewServer(
|
140 | |
context.Background(),
|
141 | 136 |
originURL,
|
142 | 137 |
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
|
143 | 138 |
r.Header.Add(headerKey, headerVal)
|