Created
September 9, 2018 04:26
-
-
Save amsokol/e52d2c3e601b128e9d02a2a5f1ad1fd5 to your computer and use it in GitHub Desktop.
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 grpc | |
import ( | |
"context" | |
"log" | |
"net" | |
"os" | |
"os/signal" | |
"google.golang.org/grpc" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/api/v1" | |
) | |
// RunServer runs gRPC service to publish ToDo service | |
func RunServer(ctx context.Context, v1API v1.ToDoServiceServer, port string) error { | |
listen, err := net.Listen("tcp", ":"+port) | |
if err != nil { | |
return err | |
} | |
// register service | |
server := grpc.NewServer() | |
v1.RegisterToDoServiceServer(server, v1API) | |
// graceful shutdown | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
for range c { | |
// sig is a ^C, handle it | |
log.Println("shutting down gRPC server...") | |
server.GracefulStop() | |
<-ctx.Done() | |
} | |
}() | |
// start gRPC server | |
log.Println("starting gRPC server...") | |
return server.Serve(listen) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment