-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 2.1 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/v1jayr/grpc-hello-world/lib/grpc/proto"
"github.com/v1jayr/grpc-hello-world/lib/server"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
defaultGrpcPort = 9090
defaultHttpPort = 9080
)
func main() {
proxyMode := flag.Bool("proxy", false, "start reverse proxy")
grpcPort := flag.Uint("grpc-port", defaultGrpcPort, "grpc server port")
httpPort := flag.Uint("http-port", defaultHttpPort, "grpc server port")
flag.Parse()
defer glog.Flush()
if *proxyMode {
fmt.Printf("starting HTTP gateway at port=%d\n", *httpPort)
startHttpReverseProxy(*grpcPort, *httpPort)
} else {
fmt.Printf("starting gRPC server at port=%d\n", *grpcPort)
startGrpcServer(*grpcPort)
}
}
func startGrpcServer(port uint) {
grpcServer := grpc.NewServer()
helloWorldImpl := server.HelloWorldServer{}
proto.RegisterHelloWorldServer(grpcServer, helloWorldImpl)
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer.Serve(lis)
}
func startHttpReverseProxy(grpcPort, httpPort uint) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
option := runtime.WithErrorHandler(func(ctx context.Context, sm *runtime.ServeMux, m runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
b, errx := io.ReadAll(r.Body)
if errx != nil {
fmt.Printf("failed to read req, %v\n", errx)
}
fmt.Printf("[Method=%s] [URL=%s] Request=%s\n", r.Method, r.RequestURI, string(b))
fmt.Printf("%v\n", err)
w.WriteHeader(500)
w.Write([]byte("Internal error"))
})
mux := runtime.NewServeMux(option)
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
endpoint := fmt.Sprintf("localhost:%d", grpcPort)
if err := proto.RegisterHelloWorldHandlerFromEndpoint(ctx, mux, endpoint, opts); err != nil {
return err
}
return http.ListenAndServe(fmt.Sprintf(":%d", httpPort), mux)
}