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
#!/bin/bash | |
# generate ca.key | |
openssl genrsa -out ca.key 4096 | |
# generate certificate | |
openssl req -new -x509 -key ca.key -sha256 -subj "/C=SE/ST=HL/O=Example, INC." -days 365 -out ca.cert | |
# generate the server key | |
openssl genrsa -out server.key 4096 | |
# Generate the csr | |
openssl req -new -key server.key -out server.csr -config certificate.conf |
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
// 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 | |
} | |
s := grpc.NewServer( | |
grpc.Creds(cred), | |
) |
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
syntax = "proto3"; | |
package main; | |
option go_package=".;pingpong"; | |
message PingRequest { | |
} | |
message PongResponse { | |
bool ok = 1; | |
} |
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
// PingPongServer is the server API for PingPong service. | |
// All implementations must embed UnimplementedPingPongServer | |
// for forward compatibility | |
type PingPongServer interface { | |
Ping(context.Context, *PingRequest) (*PongResponse, error) | |
mustEmbedUnimplementedPingPongServer() | |
} |
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
package main | |
import ( | |
"context" | |
pingpong "github.com/percybolmer/grpcexample/pingpong" | |
) | |
// Server is the Logic handler for the server | |
// It has to fullfill the GRPC schema generated Interface |
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
package main | |
import ( | |
"log" | |
"net" | |
"time" | |
pingpong "github.com/percybolmer/grpcexample/pingpong" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/credentials" | |
) |
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
type grpcMultiplexer struct { | |
*grpcweb.WrappedGrpcServer | |
} | |
// Handler is used to route requests to either grpc or to regular http | |
func (m *grpcMultiplexer) Handler(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
if m.IsGrpcWebRequest(r) { | |
m.ServeHTTP(w, r) | |
return |
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
// loadTLSCfg will load a certificate and create a tls config | |
func loadTLSCfg() *tls.Config { | |
b, _ := ioutil.ReadFile("../cert/server.crt") | |
cp := x509.NewCertPool() | |
if !cp.AppendCertsFromPEM(b) { | |
log.Fatal("credentials: failed to append certificates") | |
} | |
config := &tls.Config{ | |
InsecureSkipVerify: false, | |
RootCAs: cp, |
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
package main | |
import ( | |
"context" | |
"crypto/tls" | |
"crypto/x509" | |
"io/ioutil" | |
"log" | |
"github.com/percybolmer/grpcexample/pingpong" |
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
func main() { | |
// We Generate a TLS grpc API | |
apiserver, err := GenerateTLSApi("cert/server.crt", "cert/server.key") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Start listening on a TCP Port | |
lis, err := net.Listen("tcp", "127.0.0.1:9990") | |
if err != nil { |
OlderNewer