forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
165 lines (135 loc) · 2.85 KB
/
proxy.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
// Package proxy is a broker using the micro proxy
package proxy
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/cmd"
)
type sidecar struct {
opts broker.Options
}
func init() {
cmd.DefaultBrokers["sidecar"] = NewBroker
}
func newBroker(opts ...broker.Option) broker.Broker {
var options broker.Options
for _, o := range opts {
o(&options)
}
var addrs []string
for _, addr := range options.Addrs {
if len(addr) == 0 {
continue
}
addrs = append(addrs, addr)
}
if len(addrs) == 0 {
addrs = []string{"localhost:8081"}
}
broker.Addrs(addrs...)(&options)
return &sidecar{
opts: options,
}
}
func (s *sidecar) Options() broker.Options {
return s.opts
}
func (s *sidecar) Address() string {
if len(s.opts.Addrs) == 0 {
return "localhost:8081"
}
return s.opts.Addrs[0]
}
func (s *sidecar) Connect() error {
return nil
}
func (s *sidecar) Disconnect() error {
return nil
}
func (s *sidecar) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&s.opts)
}
var addrs []string
for _, addr := range s.opts.Addrs {
if len(addr) == 0 {
continue
}
addrs = append(addrs, addr)
}
if len(addrs) == 0 {
addrs = []string{"localhost:8081"}
}
broker.Addrs(addrs...)(&s.opts)
return nil
}
func (s *sidecar) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
pub := func(addr string) error {
scheme := "http"
if s.opts.Secure {
scheme = "https"
}
url := fmt.Sprintf("%s://%s/broker?topic=%s", scheme, addr, topic)
req, err := http.NewRequest("POST", url, bytes.NewReader(msg.Body))
if err != nil {
return err
}
for k, v := range msg.Header {
req.Header.Set(k, v)
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
// discard response
io.Copy(ioutil.Discard, rsp.Body)
rsp.Body.Close()
return nil
}
var gerr error
for _, addr := range s.opts.Addrs {
if err := pub(addr); err != nil {
gerr = err
continue
}
return nil
}
return gerr
}
func (s *sidecar) Subscribe(topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
var options broker.SubscribeOptions
for _, o := range opts {
o(&options)
}
sub := func(addr string) (broker.Subscriber, error) {
scheme := "ws"
if s.opts.Secure {
scheme = "wss"
}
url := fmt.Sprintf("%s://%s/broker?topic=%s", scheme, addr, topic)
if len(options.Queue) > 0 {
url = fmt.Sprintf("%s&queue=%s", url, options.Queue)
}
return newSubscriber(url, topic, h, options)
}
var gerr error
for _, addr := range s.opts.Addrs {
s, err := sub(addr)
if err != nil {
gerr = err
continue
}
return s, nil
}
return nil, gerr
}
func (s *sidecar) String() string {
return "sidecar"
}
func NewBroker(opts ...broker.Option) broker.Broker {
return newBroker(opts...)
}