aboutsummaryrefslogtreecommitdiff
path: root/client/main.go
diff options
context:
space:
mode:
authorCarlos Sosa <gnusosa@gnusosa.net>2020-06-22 11:33:39 -0700
committerCarlos Sosa <gnusosa@gnusosa.net>2020-06-22 11:33:39 -0700
commit0b609156b184d00ab1a6e742b9b998be4457345e (patch)
tree9283646735cf0f7a984ff0fe4013f6abc7a8be4f /client/main.go
First Commit
Diffstat (limited to 'client/main.go')
-rw-r--r--client/main.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/client/main.go b/client/main.go
new file mode 100644
index 0000000..d571fef
--- /dev/null
+++ b/client/main.go
@@ -0,0 +1,60 @@
+package main
+
+import (
+ "log"
+
+ "gitlab.com/pantomath-io/demo-grpc/api"
+ "golang.org/x/net/context"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/credentials"
+)
+
+// Authentication holds the login/password
+type Authentication struct {
+ Login string
+ Password string
+}
+
+// GetRequestMetadata gets the current request metadata
+func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
+ return map[string]string{
+ "login": a.Login,
+ "password": a.Password,
+ }, nil
+}
+
+// RequireTransportSecurity indicates whether the credentials requires transport security
+func (a *Authentication) RequireTransportSecurity() bool {
+ return true
+}
+
+func main() {
+ var conn *grpc.ClientConn
+
+ // Create the client TLS credentials
+ creds, err := credentials.NewClientTLSFromFile("cert/server.crt", "")
+ if err != nil {
+ log.Fatalf("could not load tls cert: %s", err)
+ }
+
+ // Setup the login/pass
+ auth := Authentication{
+ Login: "john",
+ Password: "doe",
+ }
+
+ // Initiate a connection with the server
+ conn, err = grpc.Dial("localhost:7777", grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
+ if err != nil {
+ log.Fatalf("did not connect: %s", err)
+ }
+ defer conn.Close()
+
+ c := api.NewPingClient(conn)
+
+ response, err := c.SayHello(context.Background(), &api.PingMessage{Greeting: "foo"})
+ if err != nil {
+ log.Fatalf("error when calling SayHello: %s", err)
+ }
+ log.Printf("Response from server: %s", response.Greeting)
+}