-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
49 lines (43 loc) · 1.14 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
package republique
import (
"context"
"fmt"
rp "github.com/steveoc64/republique5/proto"
"net"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func (s *Server) grpcRun() {
endpoint := fmt.Sprintf(":%d", s.port)
lis, err := net.Listen("tcp", endpoint)
if err != nil {
s.log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
rp.RegisterGameServiceServer(grpcServer, s)
s.log.WithFields(logrus.Fields{
"port": s.port,
"endpoint": endpoint,
}).Println("Serving gRPC")
grpcServer.Serve(lis)
}
func (s *Server) rpcProxy() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
rpcendpoint := fmt.Sprintf(":%d", s.port)
webendpoint := fmt.Sprintf(":%d", s.web)
err := rp.RegisterGameServiceHandlerFromEndpoint(ctx, mux, rpcendpoint, opts)
if err != nil {
return err
}
s.log.WithFields(logrus.Fields{
"port": s.web,
"endpoint": webendpoint,
}).Println("Serving REST Proxy")
return http.ListenAndServe(webendpoint, mux)
}