forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
executable file
·211 lines (177 loc) · 5.95 KB
/
handler.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
206
207
208
209
210
211
package handler
import (
"context"
"time"
pb "github.com/2637309949/micro/v3/proto/registry"
"github.com/2637309949/micro/v3/service"
"github.com/2637309949/micro/v3/service/errors"
log "github.com/2637309949/micro/v3/service/logger"
"github.com/2637309949/micro/v3/service/registry"
"github.com/2637309949/micro/v3/service/registry/util"
"github.com/2637309949/micro/v3/util/auth/namespace"
)
type Registry struct {
// service id
ID string
// the event
Event *service.Event
}
func ActionToEventType(action string) registry.EventType {
switch action {
case "create":
return registry.Create
case "delete":
return registry.Delete
default:
return registry.Update
}
}
func (r *Registry) publishEvent(action string, service *pb.Service) error {
// TODO: timestamp should be read from received event
// Right now registry.Result does not contain timestamp
event := &pb.Event{
Id: r.ID,
Type: pb.EventType(ActionToEventType(action)),
Timestamp: time.Now().UnixNano(),
Service: service,
}
log.Debugf("publishing event %s for action %s", event.Id, action)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return r.Event.Publish(ctx, event)
}
// GetService from the registry with the name requested
func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse) error {
// parse the options
var options registry.GetOptions
if req.Options != nil && len(req.Options.Domain) > 0 {
options.Domain = req.Options.Domain
} else {
options.Domain = registry.DefaultDomain
}
// authorize the request. Non admins can also do this
publicNS := namespace.Public(registry.DefaultDomain)
if err := namespace.Authorize(ctx, options.Domain, "registry.Registry.GetService", publicNS); err != nil {
return err
}
// get the services in the namespace
services, err := registry.DefaultRegistry.GetService(req.Service, registry.GetDomain(options.Domain))
if err == registry.ErrNotFound || len(services) == 0 {
return errors.NotFound("registry.Registry.GetService", registry.ErrNotFound.Error())
} else if err != nil {
return errors.InternalServerError("registry.Registry.GetService", err.Error())
}
// serialize the response
rsp.Services = make([]*pb.Service, len(services))
for i, srv := range services {
rsp.Services[i] = util.ToProto(srv)
}
return nil
}
// Register a service
func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error {
var opts []registry.RegisterOption
var domain string
// parse the options
if req.Options != nil && req.Options.Ttl > 0 {
ttl := time.Duration(req.Options.Ttl) * time.Second
opts = append(opts, registry.RegisterTTL(ttl))
}
if req.Options != nil && len(req.Options.Domain) > 0 {
domain = req.Options.Domain
} else {
domain = registry.DefaultDomain
}
opts = append(opts, registry.RegisterDomain(domain))
// authorize the request
if err := namespace.AuthorizeAdmin(ctx, domain, "registry.Registry.Register"); err != nil {
return err
}
// register the service
if err := registry.DefaultRegistry.Register(util.ToService(req), opts...); err != nil {
return errors.InternalServerError("registry.Registry.Register", err.Error())
}
// publish the event
go r.publishEvent("create", req)
return nil
}
// Deregister a service
func (r *Registry) Deregister(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error {
// parse the options
var domain string
if req.Options != nil && len(req.Options.Domain) > 0 {
domain = req.Options.Domain
} else {
domain = registry.DefaultDomain
}
// authorize the request
if err := namespace.AuthorizeAdmin(ctx, domain, "registry.Registry.Deregister"); err != nil {
return err
}
// deregister the service
if err := registry.DefaultRegistry.Deregister(util.ToService(req), registry.DeregisterDomain(domain)); err != nil {
return errors.InternalServerError("registry.Registry.Deregister", err.Error())
}
// publish the event
go r.publishEvent("delete", req)
return nil
}
// ListServices returns all the services
func (r *Registry) ListServices(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
// parse the options
var domain string
if req.Options != nil && len(req.Options.Domain) > 0 {
domain = req.Options.Domain
} else {
domain = registry.DefaultDomain
}
// authorize the request. Non admins can also do this
publicNS := namespace.Public(registry.DefaultDomain)
if err := namespace.Authorize(ctx, domain, "registry.Registry.ListServices", publicNS); err != nil {
return err
}
// list the services from the registry
services, err := registry.DefaultRegistry.ListServices(registry.ListDomain(domain))
if err != nil {
return errors.InternalServerError("registry.Registry.ListServices", err.Error())
}
// serialize the response
rsp.Services = make([]*pb.Service, len(services))
for i, srv := range services {
rsp.Services[i] = util.ToProto(srv)
}
return nil
}
// Watch a service for changes
func (r *Registry) Watch(ctx context.Context, req *pb.WatchRequest, rsp pb.Registry_WatchStream) error {
// parse the options
var domain string
if req.Options != nil && len(req.Options.Domain) > 0 {
domain = req.Options.Domain
} else {
domain = registry.DefaultDomain
}
// authorize the request
publicNS := namespace.Public(registry.DefaultDomain)
if err := namespace.Authorize(ctx, domain, "registry.Registry.Watch", publicNS); err != nil {
return err
}
// setup the watcher
watcher, err := registry.DefaultRegistry.Watch(registry.WatchService(req.Service), registry.WatchDomain(domain))
if err != nil {
return errors.InternalServerError("registry.Registry.Watch", err.Error())
}
for {
next, err := watcher.Next()
if err != nil {
return errors.InternalServerError("registry.Registry.Watch", err.Error())
}
err = rsp.Send(&pb.Result{
Action: next.Action,
Service: util.ToProto(next.Service),
})
if err != nil {
return errors.InternalServerError("registry.Registry.Watch", err.Error())
}
}
}