-
Notifications
You must be signed in to change notification settings - Fork 25
/
status-service.go
52 lines (44 loc) · 1.44 KB
/
status-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
package status
import (
"context"
"errors"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
statusv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/status/v1"
"github.com/fluxninja/aperture/pkg/log"
)
// StatusService is the implementation of the statusv1.StatusServiceServer interface.
type StatusService struct {
statusv1.UnimplementedStatusServiceServer
registry Registry
}
// RegisterStatusService registers the StatusService implementation with the provided grpc server.
func RegisterStatusService(server *grpc.Server, reg Registry) {
svc := &StatusService{
registry: reg,
}
statusv1.RegisterStatusServiceServer(server, svc)
}
// GetGroupStatus returns the group status for the requested group in the Registry.
func (svc *StatusService) GetGroupStatus(ctx context.Context, req *statusv1.GroupStatusRequest) (*statusv1.GroupStatus, error) {
log.Trace().Interface("path", req.Path).Msg("Received request on GetGroupStatus handler")
// extract keys from the path, separated by /
keys := strings.Split(req.Path, "/")
registry := svc.registry
for _, key := range keys {
if key == "" {
continue
}
registry = registry.ChildIfExists(key)
if registry == nil {
return &statusv1.GroupStatus{}, nil
}
}
var err error
if registry.HasError() {
err = errors.New("status registry has an error")
_ = grpc.SetHeader(ctx, metadata.Pairs("x-http-code", "503"))
}
return registry.GetGroupStatus(), err
}