-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (166 loc) · 4.91 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"encoding/json"
"errors"
"log"
"strings"
"flag"
"net/http"
"github.com/go-kit/kit/endpoint"
natstransport "github.com/go-kit/kit/transport/nats"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/nats-io/nats.go"
)
// StringService provides operations on strings.
type StringService interface {
Uppercase(context.Context, string) (string, error)
Count(context.Context, string) int
}
// stringService is a concrete implementation of StringService
type stringService struct{}
func (stringService) Uppercase(_ context.Context, s string) (string, error) {
if s == "" {
return "", ErrEmpty
}
return strings.ToUpper(s), nil
}
func (stringService) Count(_ context.Context, s string) int {
return len(s)
}
// ErrEmpty is returned when an input string is empty.
var ErrEmpty = errors.New("empty string")
// For each method, we define request and response structs
type uppercaseRequest struct {
S string `json:"s"`
}
type uppercaseResponse struct {
V string `json:"v"`
Err string `json:"err,omitempty"` // errors don't define JSON marshaling
}
type countRequest struct {
S string `json:"s"`
}
type countResponse struct {
V int `json:"v"`
}
// Endpoints are a primary abstraction in go-kit. An endpoint represents a single RPC (method in our service interface)
func makeUppercaseHTTPEndpoint(nc *nats.Conn) endpoint.Endpoint {
return natstransport.NewPublisher(
nc,
"stringsvc.uppercase",
natstransport.EncodeJSONRequest,
decodeUppercaseResponse,
).Endpoint()
}
func makeCountHTTPEndpoint(nc *nats.Conn) endpoint.Endpoint {
return natstransport.NewPublisher(
nc,
"stringsvc.count",
natstransport.EncodeJSONRequest,
decodeCountResponse,
).Endpoint()
}
func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(uppercaseRequest)
v, err := svc.Uppercase(ctx, req.S)
if err != nil {
return uppercaseResponse{v, err.Error()}, nil
}
return uppercaseResponse{v, ""}, nil
}
}
func makeCountEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(countRequest)
v := svc.Count(ctx, req.S)
return countResponse{v}, nil
}
}
// Transports expose the service to the network. In this fourth example we utilize JSON over NATS and HTTP.
func main() {
svc := stringService{}
natsURL := flag.String("nats-url", nats.DefaultURL, "URL for connection to NATS")
flag.Parse()
nc, err := nats.Connect(*natsURL)
if err != nil {
log.Fatal(err)
}
defer nc.Close()
uppercaseHTTPHandler := httptransport.NewServer(
makeUppercaseHTTPEndpoint(nc),
decodeUppercaseHTTPRequest,
httptransport.EncodeJSONResponse,
)
countHTTPHandler := httptransport.NewServer(
makeCountHTTPEndpoint(nc),
decodeCountHTTPRequest,
httptransport.EncodeJSONResponse,
)
uppercaseHandler := natstransport.NewSubscriber(
makeUppercaseEndpoint(svc),
decodeUppercaseRequest,
natstransport.EncodeJSONResponse,
)
countHandler := natstransport.NewSubscriber(
makeCountEndpoint(svc),
decodeCountRequest,
natstransport.EncodeJSONResponse,
)
uSub, err := nc.QueueSubscribe("stringsvc.uppercase", "stringsvc", uppercaseHandler.ServeMsg(nc))
if err != nil {
log.Fatal(err)
}
defer uSub.Unsubscribe()
cSub, err := nc.QueueSubscribe("stringsvc.count", "stringsvc", countHandler.ServeMsg(nc))
if err != nil {
log.Fatal(err)
}
defer cSub.Unsubscribe()
http.Handle("/uppercase", uppercaseHTTPHandler)
http.Handle("/count", countHTTPHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func decodeUppercaseHTTPRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request uppercaseRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func decodeCountHTTPRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request countRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func decodeUppercaseResponse(_ context.Context, msg *nats.Msg) (interface{}, error) {
var response uppercaseResponse
if err := json.Unmarshal(msg.Data, &response); err != nil {
return nil, err
}
return response, nil
}
func decodeCountResponse(_ context.Context, msg *nats.Msg) (interface{}, error) {
var response countResponse
if err := json.Unmarshal(msg.Data, &response); err != nil {
return nil, err
}
return response, nil
}
func decodeUppercaseRequest(_ context.Context, msg *nats.Msg) (interface{}, error) {
var request uppercaseRequest
if err := json.Unmarshal(msg.Data, &request); err != nil {
return nil, err
}
return request, nil
}
func decodeCountRequest(_ context.Context, msg *nats.Msg) (interface{}, error) {
var request countRequest
if err := json.Unmarshal(msg.Data, &request); err != nil {
return nil, err
}
return request, nil
}