-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
51 lines (40 loc) · 1.05 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
package main
import (
"context"
"fmt"
"net"
"github.com/seth-epps/hello-go/protos"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
)
const authorityMeta = ":authority"
type grpcServer struct {
protos.UnimplementedHelloServer
}
func (s *grpcServer) SayHello(ctx context.Context, req *protos.HelloRequest) (*protos.HelloResponse, error) {
msg := "Hello From Go!"
peer, _ := peer.FromContext(ctx)
ip := peer.Addr.String()
authority := ""
if metaAuth := metadata.ValueFromIncomingContext(ctx, authorityMeta); len(metaAuth) != 0 {
authority = metaAuth[0]
}
return &protos.HelloResponse{
Ip: &ip,
Message: &msg,
Authority: &authority,
}, nil
}
func startGrpc() error {
lis, err := net.Listen("tcp", ":9090")
if err != nil {
return fmt.Errorf("failed to listen on port 9090: %w", err)
}
s := grpc.NewServer()
protos.RegisterHelloServer(s, &grpcServer{})
reflection.Register(s)
fmt.Printf("gRPC server listening at %v\n", lis.Addr())
return s.Serve(lis)
}