From: Julien Andrieux Date: Thu, 5 Oct 2017 15:36:05 +0000 (+0200) Subject: Add SSL support on the server X-Git-Url: https://git.halfball.org/?a=commitdiff_plain;h=4f4c8a339fcefdc84577e1a72c5ffa5c5516f473;p=demo-grpc.git Add SSL support on the server --- diff --git a/server/main.go b/server/main.go index b21e631..7b3a3e1 100644 --- a/server/main.go +++ b/server/main.go @@ -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)