-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
102 lines (88 loc) · 2.88 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
package gateway
import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"mime"
"net/http"
"os"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/rakyll/statik/fs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"github.com/wellness-reservator/wellness-ws/config"
"github.com/wellness-reservator/wellness-ws/insecure"
pbWellness "github.com/wellness-reservator/wellness-ws/proto"
// Static files
_ "github.com/wellness-reservator/wellness-ws/statik"
)
// 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")
statikFS, err := fs.New()
if err != nil {
// Panic since this is a permanent error.
panic("creating OpenAPI filesystem: " + err.Error())
}
return http.FileServer(statikFS)
}
func CustomMatcher(key string) (string, bool) {
switch key {
case "X-Userinfo":
return key, true
default:
return runtime.DefaultHeaderMatcher(key)
}
}
// 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)
// Create a client connection to the gRPC Server we just started.
// This is where the gRPC-Gateway proxies the requests.
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)
}
gwmux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(CustomMatcher))
err = pbWellness.RegisterWellnessServiceHandler(context.Background(), gwmux, conn)
if err != nil {
return fmt.Errorf("failed to register gateway: %w", err)
}
oa := getOpenAPIHandler()
port := os.Getenv("PORT")
if port == "" {
port = "11000"
}
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
}
oa.ServeHTTP(w, r)
}),
}
// Empty parameters mean use the TLS Config specified with the server.
if config.ConfigLoaded.ServeHttp {
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("", ""))
}