-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from openinfradev/support_tls
feature. support tls
- Loading branch information
Showing
4 changed files
with
127 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package grpc_server | ||
|
||
import ( | ||
"net" | ||
"strconv" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/recovery" | ||
|
||
"github.com/openinfradev/tks-common/pkg/log" | ||
|
||
) | ||
|
||
func CreateServer(port int, tlsEnabled bool, certPath string, keyPath string) (*grpc.Server, net.Listener, error) { | ||
log.Info("Starting to listen port ", port) | ||
|
||
lis, err := net.Listen("tcp", ":"+strconv.Itoa(port)) | ||
if err != nil { | ||
log.Error("failed to listen:", err) | ||
return nil, nil, err | ||
} | ||
|
||
serverOptions := []grpc.ServerOption{ | ||
grpc.UnaryInterceptor( | ||
grpc_middleware.ChainUnaryServer( | ||
grpc_recovery.UnaryServerInterceptor(), | ||
log.IOLoggingForServerSide(), | ||
), | ||
), | ||
} | ||
|
||
if tlsEnabled { | ||
log.Info("TLS enabled!!!") | ||
tlsCredentials, err := loadTLSCredentials(certPath, keyPath) | ||
if err != nil { | ||
log.Error("Cannot load TLS credentials: ", err) | ||
return nil, nil, err | ||
} | ||
serverOptions = append(serverOptions, grpc.Creds(tlsCredentials)) | ||
} | ||
|
||
return grpc.NewServer(serverOptions...), lis, nil | ||
} | ||
|
||
func loadTLSCredentials(certPath string, keyPath string) (credentials.TransportCredentials, error) { | ||
creds, err := credentials.NewServerTLSFromFile(certPath, keyPath) | ||
if err != nil { | ||
log.Error("Fail to load credentials: ", err) | ||
return nil, err | ||
} | ||
|
||
return creds, nil | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters