-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
103 lines (91 loc) · 3.5 KB
/
gateway.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
package gateway
import (
"context"
"crypto/tls"
"fmt"
"io/fs"
"io/ioutil"
"mime"
"net/http"
"os"
"strings"
"github.com/wareed1/enhanced-grpc-api-gateway-example/third_party"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/wareed1/enhanced-grpc-api-gateway-example/insecure"
usersv1 "github.com/wareed1/enhanced-grpc-api-gateway-example/gen/proto/go/public/api/users/v1"
ordersv1 "github.com/wareed1/enhanced-grpc-api-gateway-example/gen/proto/go/public/api/orders/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
)
// getOpenAPIHandler serves an OpenAPI UI.
// Adapted from https://github.com/philips/grpc-gateway-example/blob/a269bcb5931ca92be0ceae6130ac27ae89582ecc/cmd/serve.go#L63
func getOpenAPIHandler() http.Handler {
mime.AddExtensionType(".svg", "image/svg+xml")
// Use subdirectory in embedded files
subFS, err := fs.Sub(third_party.OpenAPI, "OpenAPI")
if err != nil {
panic("couldn't create sub filesystem: " + err.Error())
}
return http.FileServer(http.FS(subFS))
}
// Run runs the gRPC-Gateway, dialling the provided address.
func Run(dialAddr string) error {
// Adds gRPC internal logs. This is quite verbose, so adjust as desired!
log := grpclog.NewLoggerV2(os.Stdout, ioutil.Discard, ioutil.Discard)
grpclog.SetLoggerV2(log)
log.Info("Starting the gRPC-Gateway on address: ", dialAddr)
// Create a client connection to the gRPC Server we just started.
// This is where the gRPC-Gateway proxies the requests.
log.Info("Creating a client connection to the gRPC Server we just started ...")
conn, err := grpc.DialContext(
context.Background(),
dialAddr,
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(insecure.CertPool, "")),
grpc.WithBlock(),
)
if err != nil {
return fmt.Errorf("failed to dial server: %w", err)
}
log.Info("Creating the gateway mutex ...")
gwmux := runtime.NewServeMux()
log.Info("Registering the users service ...")
err = usersv1.RegisterUsersServiceHandler(context.Background(), gwmux, conn)
if err != nil {
return fmt.Errorf("failed to register gateway for users service: %w", err)
}
log.Info("Registering the orders service ...")
err = ordersv1.RegisterOrdersServiceHandler(context.Background(), gwmux, conn)
if err != nil {
return fmt.Errorf("failed to register gateway for orders service: %w", err)
}
log.Info("Create the OpenAPI handler ...")
oa := getOpenAPIHandler()
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
gatewayAddr := "0.0.0.0:" + port
gwServer := &http.Server{
Addr: gatewayAddr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
gwmux.ServeHTTP(w, r)
return
}
if !strings.HasPrefix(r.URL.Path, "/private") {
oa.ServeHTTP(w, r)
}
}),
}
// Empty parameters mean use the TLS Config specified with the server.
if strings.ToLower(os.Getenv("SERVE_HTTP")) == "true" {
log.Info("Serving gRPC-Gateway and OpenAPI Documentation on http://", gatewayAddr)
return fmt.Errorf("serving gRPC-Gateway server: %w", gwServer.ListenAndServe())
}
gwServer.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{insecure.Cert},
}
log.Info("Serving gRPC-Gateway and OpenAPI Documentation on https://", gatewayAddr)
return fmt.Errorf("serving gRPC-Gateway server: %w", gwServer.ListenAndServeTLS("", ""))
}