transport and tracing doc comments
Peter Bourgon
7 years ago
|
0 |
// Package opentracing provides Go kit integration to the OpenTracing project.
|
|
1 |
// OpenTracing implements a general purpose interface that microservices can
|
|
2 |
// program against, and which adapts to all major distributed tracing systems.
|
|
3 |
package opentracing
|
|
0 |
// Package grpc provides a gRPC binding for endpoints.
|
|
1 |
package grpc
|
|
0 |
// Package http provides a general purpose HTTP binding for endpoints.
|
|
1 |
package http
|
0 | |
# package transport/httprp
|
1 | |
|
2 | |
`package transport/httprp` provides an HTTP reverse-proxy transport.
|
3 | |
|
4 | |
## Rationale
|
5 | |
|
6 | |
HTTP server applications often associate multiple handlers with a single HTTP listener, each handler differentiated by the request URI and/or HTTP method. Handlers that perform business-logic in the app can implement the `Endpoint` interface and be exposed using the `package transport/http` server. Handlers that need to proxy the request to another HTTP endpoint can do so with this package by simply specifying the base URL to forward the request to.
|
7 | |
|
8 | |
## Usage
|
9 | |
|
10 | |
The following example uses the [Gorilla Mux](https://github.com/gorilla/mux) router to illustrate how a mixture of proxying and non-proxying request handlers can be used with a single listener:
|
11 | |
|
12 | |
```go
|
13 | |
import (
|
14 | |
"net/http"
|
15 | |
"net/url"
|
16 | |
|
17 | |
kithttp "github.com/go-kit/kit/transport/http"
|
18 | |
kithttprp "github.com/go-kit/kit/transport/httprp"
|
19 | |
"github.com/gorilla/mux"
|
20 | |
"golang.org/x/net/context"
|
21 | |
)
|
22 | |
|
23 | |
func main() {
|
24 | |
router := mux.NewRouter()
|
25 | |
|
26 | |
// server HTTP endpoint handled here
|
27 | |
router.Handle("/foo",
|
28 | |
kithttp.NewServer(
|
29 | |
context.Background(),
|
30 | |
func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil },
|
31 | |
func(*http.Request) (interface{}, error) { return struct{}{}, nil },
|
32 | |
func(http.ResponseWriter, interface{}) error { return nil },
|
33 | |
)).Methods("GET")
|
34 | |
|
35 | |
// proxy endpoint, forwards requests to http://other.service.local/base/bar
|
36 | |
remoteServiceURL, _ := url.Parse("http://other.service.local/base")
|
37 | |
router.Handle("/bar",
|
38 | |
kithttprp.NewServer(
|
39 | |
context.Background(),
|
40 | |
remoteServiceURL,
|
41 | |
)).Methods("GET")
|
42 | |
|
43 | |
http.ListenAndServe(":8080", router)
|
44 | |
}
|
45 | |
```
|
46 | |
|
47 | |
You can also supply a set of `RequestFunc` functions to be run before proxying the request. This can be useful for adding request headers required by the backend system (e.g. API tokens).
|
|
0 |
// Package httprp provides an HTTP reverse-proxy transport. HTTP handlers that
|
|
1 |
// need to proxy requests to another HTTP service can do so with this package by
|
|
2 |
// specifying the URL to forward the request to.
|
|
3 |
package httprp
|