-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
69 lines (54 loc) · 1.6 KB
/
server.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 grpc
import (
"context"
"errors"
"fmt"
v1 "github.com/alexfalkowski/konfig/api/konfig/v1"
"github.com/alexfalkowski/konfig/vcs"
verrors "github.com/alexfalkowski/konfig/vcs/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Server for gRPC.
type Server struct {
conf vcs.Configurator
v1.UnimplementedConfiguratorServiceServer
}
// GetConfig for gRPC.
func (s *Server) GetConfig(ctx context.Context, req *v1.GetConfigRequest) (*v1.GetConfigResponse, error) {
resp := &v1.GetConfigResponse{
Application: req.Application,
Version: req.Version,
Environment: req.Environment,
Command: req.Command,
ContentType: "application/yaml",
}
if err := s.validateGetConfigRequest(req); err != nil {
return resp, err
}
data, err := s.conf.GetConfig(ctx, req.Application, req.Version, req.Environment, req.Command)
if err != nil {
if errors.Is(err, verrors.ErrNotFound) {
msg := fmt.Sprintf("%s/%s/%s/%s was not found", req.Application, req.Version, req.Environment, req.Command)
return resp, status.Error(codes.NotFound, msg)
}
return resp, err
}
resp.Data = data
return resp, nil
}
func (s *Server) validateGetConfigRequest(req *v1.GetConfigRequest) error {
if req.Application == "" {
return status.Error(codes.InvalidArgument, "invalid application")
}
if req.Version == "" {
return status.Error(codes.InvalidArgument, "invalid version")
}
if req.Environment == "" {
return status.Error(codes.InvalidArgument, "invalid environment")
}
if req.Command == "" {
return status.Error(codes.InvalidArgument, "invalid command")
}
return nil
}