/
middleware.go
56 lines (50 loc) · 1.49 KB
/
middleware.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
package discovery
import (
"context"
"errors"
"fmt"
"github.com/ischenkx/kantoku/pkg/common/data/codec"
"github.com/ischenkx/kantoku/pkg/common/service"
"github.com/ischenkx/kantoku/pkg/core/event"
"golang.org/x/sync/errgroup"
)
type Middleware[Service service.Service] struct {
InfoProvider func(ctx context.Context, srvc Service) (map[string]any, error)
Events *event.Broker
RequestCodec codec.Codec[Request, []byte]
ResponseCodec codec.Codec[Response, []byte]
}
func (m Middleware[Service]) BeforeRun(ctx context.Context, g *errgroup.Group, service service.Service) {
g.Go(func() error {
typedService, ok := service.(Service)
if !ok {
return errors.New("failed to cast service to the specified type")
}
responder := &Responder[Service]{
Service: typedService,
InfoProvider: m.InfoProvider,
Events: m.Events,
RequestCodec: m.RequestCodec,
ResponseCodec: m.ResponseCodec,
}
if err := responder.Run(ctx); err != nil {
return fmt.Errorf("failed to start a service discovery responder: %w", err)
}
return nil
})
}
func WithStaticInfo[Service service.Service](
info map[string]any,
events *event.Broker,
requestCodec codec.Codec[Request, []byte],
responseCodec codec.Codec[Response, []byte],
) Middleware[Service] {
return Middleware[Service]{
InfoProvider: func(ctx context.Context, srvc Service) (map[string]any, error) {
return info, nil
},
Events: events,
RequestCodec: requestCodec,
ResponseCodec: responseCodec,
}
}