Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 6, 2021 11:37
Show Gist options
  • Save percybolmer/1594156e1881ea739635b4b3483c7382 to your computer and use it in GitHub Desktop.
Save percybolmer/1594156e1881ea739635b4b3483c7382 to your computer and use it in GitHub Desktop.
// GenerateTLSApi will load TLS certificates and key and create a grpc server with those.
func GenerateTLSApi(pemPath, keyPath string) (*grpc.Server, error) {
cred, err := credentials.NewServerTLSFromFile(pemPath, keyPath)
if err != nil {
return nil, err
}
// Add PingCounter from the interceptors package
pc := interceptors.PingCounter{}
s := grpc.NewServer(
grpc.Creds(cred),
// Here we add the chaining of interceptors, they will execute in order
grpc.ChainUnaryInterceptor(
pc.ServerCount,
interceptors.LogRequest,
),
)
return s, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment