53 lines
1.0 KiB
Go
Raw Normal View History

2021-01-10 15:31:37 +08:00
// grpc server
package main
import (
2021-01-17 05:25:49 +08:00
"crypto/tls"
"crypto/x509"
"io/ioutil"
2021-01-10 15:31:37 +08:00
"net"
2021-01-17 05:25:49 +08:00
log "github.com/sirupsen/logrus"
2021-01-10 15:31:37 +08:00
"google.golang.org/grpc"
2021-01-17 05:25:49 +08:00
"google.golang.org/grpc/credentials"
2021-01-16 16:28:32 +08:00
pb "github.com/esinio/geco/gen/proto/echo/v1"
srv "github.com/esinio/geco/service/echo/v1"
2021-01-10 15:31:37 +08:00
)
func main() {
2021-01-17 05:25:49 +08:00
cert, err := tls.LoadX509KeyPair("./cert/server.pem", "./cert/server.key")
if err != nil {
log.Fatal(err)
}
certPool := x509.NewCertPool()
ca, err := ioutil.ReadFile("./cert/ca.pem")
if err != nil {
log.Fatal(err)
}
if ok := certPool.AppendCertsFromPEM(ca); !ok {
log.Fatal("failed to append certs")
}
creds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.RequireAndVerifyClientCert, // NOTE: this is optional!
ClientCAs: certPool,
})
s := grpc.NewServer(grpc.Creds(creds))
2021-01-16 16:28:32 +08:00
pb.RegisterEchoServiceServer(s, srv.NewService())
2021-01-10 15:31:37 +08:00
lis, err := net.Listen("tcp", ":9090")
if err != nil {
panic(err)
}
defer lis.Close()
defer s.GracefulStop()
if err := s.Serve(lis); err != nil {
panic(err)
}
}