Add SSL support on the server
authorJulien Andrieux <julien@pantomath.io>
Thu, 5 Oct 2017 15:36:05 +0000 (17:36 +0200)
committerJulien Andrieux <julien@pantomath.io>
Thu, 5 Oct 2017 15:36:05 +0000 (17:36 +0200)
server/main.go

index b21e631..7b3a3e1 100644 (file)
@@ -7,12 +7,13 @@ import (
 
        "gitlab.com/pantomath-io/demo-grpc/api"
        "google.golang.org/grpc"
+       "google.golang.org/grpc/credentials"
 )
 
 // main start a gRPC server and waits for connection
 func main() {
        // create a listener on TCP port 7777
-       lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 7777))
+       lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "localhost", 7777))
        if err != nil {
                log.Fatalf("failed to listen: %v", err)
        }
@@ -20,8 +21,17 @@ func main() {
        // create a server instance
        s := api.Server{}
 
+       // Create the TLS credentials
+       creds, err := credentials.NewServerTLSFromFile("cert/server.crt", "cert/server.key")
+       if err != nil {
+               log.Fatalf("could not load TLS keys: %s", err)
+       }
+
+       // Create an array of gRPC options with the credentials
+       opts := []grpc.ServerOption{grpc.Creds(creds)}
+
        // create a gRPC server object
-       grpcServer := grpc.NewServer()
+       grpcServer := grpc.NewServer(opts...)
 
        // attach the Ping service to the server
        api.RegisterPingServer(grpcServer, &s)