-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
61 lines (53 loc) · 1.44 KB
/
main.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
package main
import (
"bytes"
context "context"
"net"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/grpc-ecosystem/go-grpc-middleware"
grpc_logurs "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func main() {
lis := must(net.Listen("tcp", ":12345"))
log := logrus.New()
srv := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_logurs.StreamServerInterceptor(logrus.NewEntry(log)),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_logurs.UnaryServerInterceptor(logrus.NewEntry(log)),
)),
)
svc := new(helloService)
RegisterHelloServiceServer(srv, svc)
println("serving at :12345")
srv.Serve(lis)
}
type helloService struct{}
// SayHello implements HelloServiceServer
func (*helloService) SayHello(ctx context.Context, req *HelloRequest) (*HelloResponse, error) {
var b bytes.Buffer
b.WriteString(req.Greet)
b.WriteString("\n\n")
spew.Fdump(&b, os.Environ())
md, _ := metadata.FromIncomingContext(ctx)
spew.Fdump(&b, md)
return &HelloResponse{
GreetBack: b.String(),
}, nil
}
// mustEmbedUnimplementedHelloServiceServer implements HelloServiceServer
func (*helloService) mustEmbedUnimplementedHelloServiceServer() {
panic("unimplemented")
}
var _ HelloServiceServer = (*helloService)(nil)
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}