-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
71 lines (60 loc) · 2.01 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
70
71
package main
import (
"context"
"log"
"net"
pb "github.com/opencopilot/haproxy-manager/manager"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
dockerClient "github.com/docker/docker/client"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
)
type server struct {
dockerCli *dockerClient.Client
}
func (s *server) GetStatus(ctx context.Context, in *pb.ManagerStatusRequest) (*pb.ManagerStatus, error) {
// return the status of the manager - what should this contain?
return &pb.ManagerStatus{}, nil
}
func (s *server) Configure(ctx context.Context, in *pb.ConfigureRequest) (*pb.ManagerStatus, error) {
// execute the configuration change on the service (HAProxy)
// configureService(s.dockerCli)
return &pb.ManagerStatus{}, nil
}
func startServer(dockerCli *dockerClient.Client) {
lis, err := net.Listen("tcp", ":50052")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
logger, err := zap.NewProduction()
defer logger.Sync()
if err != nil {
log.Fatalf("failed to setup logger: %v", err)
}
s := grpc.NewServer(
// grpc.Creds(creds),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.StreamServerInterceptor(logger),
grpc_recovery.StreamServerInterceptor(),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.UnaryServerInterceptor(logger),
grpc_recovery.UnaryServerInterceptor(),
)),
)
pb.RegisterManagerServer(s, &server{
dockerCli: dockerCli,
})
// Register reflection service on gRPC server.
reflection.Register(s)
s.Serve(lis)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}