-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
64 lines (56 loc) · 1.62 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
package gateway
import (
"context"
"crypto/tls"
"fmt"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/sakiib/grpc-api/utils"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"io/ioutil"
"net/http"
"os"
"strings"
pb "github.com/sakiib/grpc-api/gen/go/book"
)
// Run establishes a connection with gRPC server that we're already running
func Run(dialAddr, tlsEnabled string) error {
log := grpclog.NewLoggerV2(os.Stdout, ioutil.Discard, ioutil.Discard)
grpclog.SetLoggerV2(log)
// Creates a connection with the gRPC server
connection, err := grpc.DialContext(
context.Background(),
dialAddr,
utils.GetDialOption(tlsEnabled),
grpc.WithBlock(),
)
if err != nil {
return fmt.Errorf("failed to dial server with: %s", err.Error())
}
mux := runtime.NewServeMux()
err = pb.RegisterBookServiceHandler(context.Background(), mux, connection)
if err != nil {
return fmt.Errorf("failed to register gateway with: %s", err.Error())
}
port := os.Getenv("PORT")
if port == "" {
port = utils.ProxyPort
}
// Creates the gateway server - HTTP/1 server
gwServer := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/v1") {
mux.ServeHTTP(w, r)
return
}
}),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{*utils.GetServerCerts()},
},
}
if tlsEnabled != "true" {
return fmt.Errorf("serving insecured gRPC-Gateway server: %v", gwServer.ListenAndServe())
}
return fmt.Errorf("serving secured gRPC-Gateway server: %v", gwServer.ListenAndServeTLS("", ""))
}