forked from gogo/grpc-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (110 loc) · 3.32 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"mime"
"net"
"net/http"
"os"
"github.com/gogo/gateway"
"github.com/grpc-ecosystem/go-grpc-middleware/validator"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/rakyll/statik/fs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"github.com/gogo/grpc-example/insecure"
pbExample "github.com/gogo/grpc-example/proto"
"github.com/gogo/grpc-example/server"
// Static files
_ "github.com/gogo/grpc-example/statik"
)
var (
gRPCPort = flag.Int("grpc-port", 10000, "The gRPC server port")
gatewayPort = flag.Int("gateway-port", 11000, "The gRPC-Gateway server port")
)
var log grpclog.LoggerV2
func init() {
log = grpclog.NewLoggerV2(os.Stdout, ioutil.Discard, ioutil.Discard)
grpclog.SetLoggerV2(log)
}
// serveOpenAPI serves an OpenAPI UI on /openapi-ui/
// Adapted from https://github.com/philips/grpc-gateway-example/blob/a269bcb5931ca92be0ceae6130ac27ae89582ecc/cmd/serve.go#L63
func serveOpenAPI(mux *http.ServeMux) error {
mime.AddExtensionType(".svg", "image/svg+xml")
statikFS, err := fs.New()
if err != nil {
return err
}
// Expose files in static on <host>/openapi-ui
fileServer := http.FileServer(statikFS)
prefix := "/openapi-ui/"
mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
return nil
}
func main() {
flag.Parse()
addr := fmt.Sprintf("localhost:%d", *gRPCPort)
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalln("Failed to listen:", err)
}
s := grpc.NewServer(
grpc.Creds(credentials.NewServerTLSFromCert(&insecure.Cert)),
grpc.UnaryInterceptor(grpc_validator.UnaryServerInterceptor()),
grpc.StreamInterceptor(grpc_validator.StreamServerInterceptor()),
)
pbExample.RegisterUserServiceServer(s, server.New())
// Serve gRPC Server
log.Info("Serving gRPC on https://", addr)
go func() {
log.Fatal(s.Serve(lis))
}()
// See https://github.com/grpc/grpc/blob/master/doc/naming.md
// for gRPC naming standard information.
dialAddr := fmt.Sprintf("passthrough://localhost/%s", addr)
conn, err := grpc.DialContext(
context.Background(),
dialAddr,
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(insecure.CertPool, "")),
grpc.WithBlock(),
)
if err != nil {
log.Fatalln("Failed to dial server:", err)
}
mux := http.NewServeMux()
jsonpb := &gateway.JSONPb{
EmitDefaults: true,
Indent: " ",
OrigName: true,
}
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, jsonpb),
// This is necessary to get error details properly
// marshalled in unary requests.
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler),
)
err = pbExample.RegisterUserServiceHandler(context.Background(), gwmux, conn)
if err != nil {
log.Fatalln("Failed to register gateway:", err)
}
mux.Handle("/", gwmux)
err = serveOpenAPI(mux)
if err != nil {
log.Fatalln("Failed to serve OpenAPI UI")
}
gatewayAddr := fmt.Sprintf("localhost:%d", *gatewayPort)
log.Info("Serving gRPC-Gateway on https://", gatewayAddr)
log.Info("Serving OpenAPI Documentation on https://", gatewayAddr, "/openapi-ui/")
gwServer := http.Server{
Addr: gatewayAddr,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{insecure.Cert},
},
Handler: mux,
}
log.Fatalln(gwServer.ListenAndServeTLS("", ""))
}