forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (172 loc) · 5.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
stdlog "log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/hashicorp/consul/api"
"golang.org/x/net/context"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/examples/addsvc/client/grpc"
"github.com/go-kit/kit/examples/addsvc/server"
"github.com/go-kit/kit/loadbalancer"
"github.com/go-kit/kit/loadbalancer/consul"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
)
func main() {
var (
httpAddr = flag.String("http.addr", ":8000", "Address for HTTP (JSON) server")
consulAddr = flag.String("consul.addr", "", "Consul agent address")
retryMax = flag.Int("retry.max", 3, "per-request retries to different instances")
retryTimeout = flag.Duration("retry.timeout", 500*time.Millisecond, "per-request timeout, including retries")
)
flag.Parse()
// Log domain
logger := log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC).With("caller", log.DefaultCaller)
stdlog.SetFlags(0) // flags are handled by Go kit's logger
stdlog.SetOutput(log.NewStdlibAdapter(logger)) // redirect anything using stdlib log to us
// Service discovery domain. In this example we use Consul.
consulConfig := api.DefaultConfig()
if len(*consulAddr) > 0 {
consulConfig.Address = *consulAddr
}
consulClient, err := api.NewClient(consulConfig)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
discoveryClient := consul.NewClient(consulClient)
// Context domain.
ctx := context.Background()
// Set up our routes.
//
// Each Consul service name maps to multiple instances of that service. We
// connect to each instance according to its pre-determined transport: in this
// case, we choose to access addsvc via its gRPC client, and stringsvc over
// plain transport/http (it has no client package).
//
// Each service instance implements multiple methods, and we want to map each
// method to a unique path on the API gateway. So, we define that path and its
// corresponding factory function, which takes an instance string and returns an
// endpoint.Endpoint for the specific method.
//
// Finally, we mount that path + endpoint handler into the router.
r := mux.NewRouter()
for consulName, methods := range map[string][]struct {
path string
factory loadbalancer.Factory
}{
"addsvc": {
{path: "/api/addsvc/concat", factory: grpc.ConcatEndpointFactory},
{path: "/api/addsvc/sum", factory: grpc.SumEndpointFactory},
},
"stringsvc": {
{path: "/api/stringsvc/uppercase", factory: httpFactory(ctx, "GET", "uppercase/")},
{path: "/api/stringsvc/concat", factory: httpFactory(ctx, "GET", "concat/")},
},
} {
for _, method := range methods {
publisher, err := consul.NewPublisher(discoveryClient, method.factory, logger, consulName)
if err != nil {
logger.Log("service", consulName, "path", method.path, "err", err)
continue
}
lb := loadbalancer.NewRoundRobin(publisher)
e := loadbalancer.Retry(*retryMax, *retryTimeout, lb)
h := makeHandler(ctx, e, logger)
r.HandleFunc(method.path, h)
}
}
// Mechanical stuff.
errc := make(chan error)
go func() {
errc <- interrupt()
}()
go func() {
logger.Log("transport", "http", "addr", *httpAddr)
errc <- http.ListenAndServe(*httpAddr, r)
}()
logger.Log("err", <-errc)
}
func makeHandler(ctx context.Context, e endpoint.Endpoint, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp, err := e(ctx, r.Body)
if err != nil {
logger.Log("err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b, ok := resp.([]byte)
if !ok {
logger.Log("err", "endpoint response is not of type []byte")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(b)
if err != nil {
logger.Log("err", err)
return
}
}
}
func makeSumEndpoint(svc server.AddService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r := request.(io.Reader)
var req server.SumRequest
if err := json.NewDecoder(r).Decode(&req); err != nil {
return nil, err
}
v := svc.Sum(req.A, req.B)
return json.Marshal(v)
}
}
func makeConcatEndpoint(svc server.AddService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r := request.(io.Reader)
var req server.ConcatRequest
if err := json.NewDecoder(r).Decode(&req); err != nil {
return nil, err
}
v := svc.Concat(req.A, req.B)
return json.Marshal(v)
}
}
func httpFactory(ctx context.Context, method, path string) loadbalancer.Factory {
return func(instance string) (endpoint.Endpoint, io.Closer, error) {
var e endpoint.Endpoint
if !strings.HasPrefix(instance, "http") {
instance = "http://" + instance
}
u, err := url.Parse(instance)
if err != nil {
return nil, nil, err
}
u.Path = path
e = httptransport.NewClient(method, u, passEncode, passDecode).Endpoint()
return e, nil, nil
}
}
func passEncode(_ context.Context, r *http.Request, request interface{}) error {
r.Body = request.(io.ReadCloser)
return nil
}
func passDecode(_ context.Context, r *http.Response) (interface{}, error) {
return ioutil.ReadAll(r.Body)
}
func interrupt() error {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
return fmt.Errorf("%s", <-c)
}