Reputation: 847
I'm migrating an existing http application to a GRPC based application. For some time, both grpc and http will be running on different ports in the same go file, and I'll be moving the existing the http server part into a goroutine. This is how it would look like.
main() {
//Move existing server into a goroutine
go func() {
s.server, err = chttp.NewHTTPSServer("443", s.certPath, s.keyPath, s.Router, s.options)
if err != nil {
log.Fatalf("server: error creating HTTP server - %v\n", err)
}
if err := s.server.Serve(); err != http.ErrServerClosed {
log.Fatalf("server: run error - %v\n", err)
}
}
// Add new grpc server
lis, err := net.Listen("tcp", ":8433")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
a := grpc.NewServer()
pb.RegisterNewServer(a, &test.GrpcServer{})
if err := a.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Could this cause any issues, can grpc, http server be run simultaneously from the same program? I'm not sure if it could cause some issues I'm not aware of.
Upvotes: 2
Views: 9328
Reputation: 3484
Yes, it is totally ok to run gRPC and HTTP servers from the same binary, as long as the ports are different.
But of course, if these two servers are doing different things, better to separate it.
And in case you wanted to do the same thing for both servers, using https://github.com/grpc-ecosystem/grpc-gateway is a common practice. It reads your protobuf definitions and generates a reverse-proxy server to translate an HTTP API into gRPC.
Upvotes: 7