@@ -2,6 +2,7 @@ package main
22
33import (
44 "context"
5+ "crypto/x509"
56 "encoding/json"
67 "fmt"
78 "log"
@@ -13,6 +14,7 @@ import (
1314 "github.com/gorilla/mux"
1415 ec "github.com/qu1queee/CodeEngine/grpc/ecommerce"
1516 "google.golang.org/grpc"
17+ "google.golang.org/grpc/credentials"
1618 "google.golang.org/grpc/credentials/insecure"
1719)
1820
@@ -98,10 +100,28 @@ func BuyHandler(w http.ResponseWriter, r *http.Request, groceryClient ec.Grocery
98100}
99101
100102func main () {
101- localEndpoint := os .Getenv ("LOCAL_ENDPOINT_WITH_PORT" )
103+ endpoint := os .Getenv ("ENDPOINT_WITH_PORT" )
104+ if endpoint == "" {
105+ log .Fatalf ("no endpoint set: %s" , endpoint )
106+ }
107+ certPool , err := x509 .SystemCertPool ()
108+ if err != nil {
109+ log .Fatalf ("failed to get cert pool: %v" , err )
110+ }
111+ creds := credentials .NewClientTLSFromCert (certPool , "" )
112+ insArg := os .Getenv ("INSECURE" )
113+ if insArg != "" {
114+ unencrypted , err := strconv .ParseBool (insArg )
115+ if err != nil {
116+ log .Fatalf ("could not parse %v: " , err )
117+ }
118+ if unencrypted {
119+ creds = insecure .NewCredentials ()
120+ }
121+ }
102122
103- fmt .Printf ("using local endpoint: %s\n " , localEndpoint )
104- conn , err := grpc .Dial (localEndpoint , grpc .WithTransportCredentials (insecure . NewCredentials () ))
123+ fmt .Printf ("using endpoint: %s\n " , endpoint )
124+ conn , err := grpc .Dial (endpoint , grpc .WithTransportCredentials (creds ))
105125 if err != nil {
106126 log .Fatalf ("failed to connect: %v" , err )
107127 }
0 commit comments