Created
February 7, 2018 14:56
-
-
Save kai5263499/37b2059eadbacda5181bec624f6b7bcd to your computer and use it in GitHub Desktop.
start a grpc server
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 Config struct { | |
GrpcPort int `config:"default=8080;usage="` | |
UseTLS bool `config:"default=false;usage="` | |
SSLCertFile string `config:"default=ssl.crt;usage=SSL public key file"` | |
SSLKeyFile string `config:"default=ssl.key;usage=SSL private key file"` | |
} | |
type Server struct { | |
conf *Config | |
contentProcessorServer *request_processor.ContentProcessorServer | |
lis *net.Listener | |
grpcServer *grpc.Server | |
} | |
func (s *Server) Start() error { | |
listenAddress := fmt.Sprintf("localhost:%d", s.conf.GrpcPort) | |
lis, err := net.Listen("tcp", listenAddress) | |
if err != nil { | |
logrus.Errorf("unable to create network listener err=%#v", err) | |
return err | |
} | |
s.lis = lis | |
logrus.Debugf("listening on %s", listenAddress) | |
if !s.conf.UseTLS { | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
return err | |
} | |
s.grpcServer = grpc.NewServer() | |
} else { | |
certFile := s.conf.SSLCertFile | |
keyFile := s.conf.SSLKeyFile | |
creds, err := credentials.NewServerTLSFromFile(certFile, keyFile) | |
if err != nil { | |
log.Fatalf("failed to listen: %v", err) | |
return err | |
} | |
s.grpcServer = grpc.NewServer(grpc.Creds(creds)) | |
} | |
pb.RegisterContentProcessorServer(s.grpcServer, s.contentProcessorServer) | |
return s.grpcServer.Serve(lis) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment