-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
54 lines (49 loc) · 1.38 KB
/
grpc.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
package main
import (
"fmt"
"log"
"net"
pb "github.com/jgadling/pennant/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// server is used to implement proto.PennantServer.
type server struct {
fc *FlagCache
}
// GetFlagValue implements proto.PennantServer
func (s *server) GetFlagValue(ctx context.Context, in *pb.FlagRequest) (*pb.FlagReply, error) {
flagCache := s.fc
logger.Debugf("Flag requested: ", in.Name)
logger.Debugf("Flag String Data: ", in.Strings)
logger.Debugf("Flag Number Data: ", in.Numbers)
flag, err := flagCache.Get(in.Name)
if err != nil {
// The flag didn't exist in the cache, let's send a 404
return &pb.FlagReply{Status: 404, Enabled: false}, nil
}
datas := make(map[string]interface{})
for k, v := range in.Strings {
datas[k] = v
}
for k, v := range in.Numbers {
datas[k] = v
}
enabled := flag.GetValue(datas)
return &pb.FlagReply{Status: 200, Enabled: enabled}, nil
}
// Run the GRPC server
func runGrpc(conf *Config, fc *FlagCache) {
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", conf.GrpcAddr, conf.GrpcPort))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterPennantServer(s, &server{fc: fc})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}