Stephane Barbarie | 7dfae95 | 2018-08-13 11:43:17 -0400 | [diff] [blame^] | 1 | package grpc |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "context" |
| 6 | "google.golang.org/grpc" |
| 7 | "google.golang.org/grpc/credentials" |
| 8 | "strconv" |
| 9 | "github.com/opencord/voltha-go/common/log" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | /* |
| 14 | To add a GRPC server to your existing component simply follow these steps: |
| 15 | |
| 16 | 1. Create a server instance by passing the host and port where it should run and optionally add certificate information |
| 17 | |
| 18 | e.g. |
| 19 | s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false) |
| 20 | |
| 21 | 2. Create a function that will register your service with the GRPC server |
| 22 | |
| 23 | e.g. |
| 24 | f := func(gs *grpc.Server) { |
| 25 | voltha.RegisterVolthaReadOnlyServiceServer( |
| 26 | gs, |
| 27 | core.NewReadOnlyServiceHandler(s.root), |
| 28 | ) |
| 29 | } |
| 30 | |
| 31 | 3. Add the service to the server |
| 32 | |
| 33 | e.g. |
| 34 | s.server.AddService(f) |
| 35 | |
| 36 | 4. Start the server |
| 37 | |
| 38 | s.server.Start(ctx) |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | |
| 43 | type GrpcServer struct { |
| 44 | gs *grpc.Server |
| 45 | address string |
| 46 | port int |
| 47 | secure bool |
| 48 | services []func(*grpc.Server) |
| 49 | |
| 50 | *GrpcSecurity |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | Instantiate a GRPC server data structure |
| 55 | */ |
| 56 | func NewGrpcServer( |
| 57 | address string, |
| 58 | port int, |
| 59 | certs *GrpcSecurity, |
| 60 | secure bool, |
| 61 | ) *GrpcServer { |
| 62 | server := &GrpcServer{ |
| 63 | address: address, |
| 64 | port: port, |
| 65 | secure: secure, |
| 66 | GrpcSecurity: certs, |
| 67 | } |
| 68 | return server |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | Start prepares the GRPC server and starts servicing requests |
| 73 | */ |
| 74 | func (s *GrpcServer) Start(ctx context.Context) { |
| 75 | host := strings.Join([]string{ |
| 76 | s.address, |
| 77 | strconv.Itoa(int(s.port)), |
| 78 | }, ":") |
| 79 | |
| 80 | lis, err := net.Listen("tcp", host) |
| 81 | if err != nil { |
| 82 | log.Fatalf("failed to listen: %v", err) |
| 83 | } |
| 84 | |
| 85 | if s.secure && s.GrpcSecurity != nil { |
| 86 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 87 | if err != nil { |
| 88 | log.Fatalf("could not load TLS keys: %s", err) |
| 89 | } |
| 90 | s.gs = grpc.NewServer(grpc.Creds(creds)) |
| 91 | |
| 92 | } else { |
| 93 | log.Info("starting-insecure-grpc-server") |
| 94 | s.gs = grpc.NewServer() |
| 95 | } |
| 96 | |
| 97 | // Register all required services |
| 98 | for _, service := range s.services { |
| 99 | service(s.gs) |
| 100 | } |
| 101 | |
| 102 | if err := s.gs.Serve(lis); err != nil { |
| 103 | log.Fatalf("failed to serve: %v\n", err) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | Stop servicing GRPC requests |
| 109 | */ |
| 110 | func (s *GrpcServer) Stop() { |
| 111 | s.gs.Stop() |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | AddService appends a generic service request function |
| 116 | */ |
| 117 | func (s *GrpcServer) AddService( |
| 118 | registerFunction func(*grpc.Server), |
| 119 | ) { |
| 120 | s.services = append(s.services, registerFunction) |
| 121 | } |