-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
69 lines (55 loc) · 1.65 KB
/
service.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
package kit
type ServiceDescriptor interface {
Generate() Service
}
// Service defines a set of RPC handlers which usually they are related to one service.
// Name must be unique per each Gateway.
type Service interface {
// Name of the service which must be unique per EdgeServer.
Name() string
// Contracts return a list of APIs which this service provides.
Contracts() []Contract
}
// ServiceWrapper lets you add customizations to your service. A specific case of it is serviceInterceptor
// which can add Pre- and Post-handlers to all the Contracts of the Service.
type ServiceWrapper interface {
Wrap(s Service) Service
}
type ServiceWrapperFunc func(Service) Service
func (sw ServiceWrapperFunc) Wrap(svc Service) Service {
return sw(svc)
}
// WrapService wraps a service, this is useful for adding middlewares to the service.
// Some middlewares like OpenTelemetry, Logger, ... could be added to the service using
// this function.
func WrapService(svc Service, wrappers ...ServiceWrapper) Service {
for _, w := range wrappers {
svc = w.Wrap(svc)
}
return svc
}
func WrapServiceContracts(svc Service, wrapper ...ContractWrapper) Service {
sw := &serviceWrap{
name: svc.Name(),
}
for _, c := range svc.Contracts() {
for _, w := range wrapper {
c = w.Wrap(c)
}
sw.contracts = append(sw.contracts, c)
}
return sw
}
// serviceWrap implements Service interface and is useful when we need to wrap another
// service.
type serviceWrap struct {
name string
contracts []Contract
}
var _ Service = (*serviceWrap)(nil)
func (sw serviceWrap) Contracts() []Contract {
return sw.contracts
}
func (sw serviceWrap) Name() string {
return sw.name
}